【问题标题】:Batching and chunking excel files (Laravel Maatwebsite library)批处理和分块 excel 文件(Laravel Maatwebsite 库)
【发布时间】:2017-09-26 03:13:37
【问题描述】:

根据documentation,可以使用批处理加载文件。您还可以在大文件的情况下对数据进行分块。

分块:

Excel::filter('chunk')->load('file.csv')->chunk(250, function($results)
{
        foreach($results as $row)
        {
            // do stuff
        }
});

批量导入:

Excel::batch('app/storage/uploads', function($rows, $file) {

    // Explain the reader how it should interpret each row,
    // for every file inside the batch
    $rows->each(function($row) {

        // Example: dump the firstname
        dd($row->firstname);

    });

});

我有一系列大文件,我想知道是否可以将这两个函数链接在一起。问题是这两个函数都需要 2 个变量(第二个是 _callback),我不知道如何将它链接在一起。

现在看来唯一对我有用的是这样的东西,但我怀疑它实际上有什么作用:

Excel::filter('chunk')->batch('files', function($rows, $file){

【问题讨论】:

    标签: php excel laravel csv maatwebsite-excel


    【解决方案1】:

    这应该可行:

    Excel::batch(request()->file, function($rows, $file) {
                    Excel::filter('chunk')->selectSheetsByIndex(0)->load($file)->chunk($this->chunkSize, function($results) {
    
                        foreach ($results as $row) {
    
                        }            
                    }, $this->shouldQueue);
                });
    

    您可以定义 chunkSize 和 shouldQueue 值。

    【讨论】:

      猜你喜欢
      • 2019-05-20
      • 1970-01-01
      • 2021-07-26
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2023-01-25
      • 2016-06-07
      相关资源
      最近更新 更多