Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用过滤器(块)会产生错误_Php_Excel_Laravel_Laravel Excel - Fatal编程技术网

Php 使用过滤器(块)会产生错误

Php 使用过滤器(块)会产生错误,php,excel,laravel,laravel-excel,Php,Excel,Laravel,Laravel Excel,我正在使用Laravel Excel 2.1。我试图使用chunk来读取一个大型导入的Excel文件,但我得到了错误方法noHeading不存在 我的原始代码 这段代码可以很好地读取导入的文件 $column_number = 5; $rows = Excel::load('storage/app/public/upload/myfiles.xlsx', function($reader) use ($column_number) { $reader->noHeading();

我正在使用Laravel Excel 2.1。我试图使用chunk来读取一个大型导入的Excel文件,但我得到了错误方法noHeading不存在

我的原始代码

这段代码可以很好地读取导入的文件

$column_number = 5;
$rows = Excel::load('storage/app/public/upload/myfiles.xlsx', function($reader) use ($column_number)
{
    $reader->noHeading();
    $reader->takeColumns($column_number);
    $reader->setDateFormat('d-m-Y');

})->get();
在文档中使用块之后

此代码给出的方法noHeading不存在。错误注意,我添加了过滤器'chunk'和chunk250


我想你需要像下面这样更新你的代码

$column_number = 5;
$rows = Excel::filter('chunk')->load('storage/app/public/upload/myfiles.xlsx')->chunk(250, function($data) use ($column_number)
 {
     foreach($data as $reader){
       $reader->noHeading();
       $reader->takeColumns($column_number);
       $reader->setDateFormat('d-m-Y');
     }
 })->get();
传递给闭包的$reader变量是ExcelParser的实例,而不是LaravelExcelReader,因此没有noHeading方法。Excel::filter确实返回一个读卡器,因此您应该能够执行以下操作:

$column_number = 5;
$rows = Excel::filter('chunk')
    ->noHeading()
    ->takeColumns($column_number)
    ->setDateFormat('d-m-Y');
    ->load('storage/app/public/upload/myfiles.xlsx')
    ->chunk(250, function($results) {return $results;});

完全没有经过测试

我刚刚尝试了代码,但它仍然显示相同的错误哦,难怪…但我得到了一个错误类型错误:函数Maatwebsite\Excel\Readers\LaravelExcelReader::chunk的参数太少,传入了1个。。。至少有2个预期的抱歉,只需在其中放入一个空函数。复制/粘贴错误。现在我收到了对成员函数get on null的另一个错误调用,当我删除get时,它返回null…显然它有contentsOk,不确定。我已经很久没有使用这个软件包了,我从来没有使用过它的块功能。它的文档记录很差,而且违反直觉,这可能就是我停止使用它的原因!我又做了一次编辑,你可以看到它发生了什么…这不是一个真正的答案,但是:如果你使用块,可能是因为你有很高的内存消耗。LaravelExcel还有一些内存友好的替代品,比如我可能会测试这个软件包,但它似乎缺少几个函数,比如noHeading、takeColumns等,或者im错误
$column_number = 5;
$rows = Excel::filter('chunk')
    ->noHeading()
    ->takeColumns($column_number)
    ->setDateFormat('d-m-Y');
    ->load('storage/app/public/upload/myfiles.xlsx')
    ->chunk(250, function($results) {return $results;});