Laravel excel在使用maatwebsite/excel读取数据时跳过行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48387402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:16:56  来源:igfitidea点击:

Laravel excell skip rows when reading data using maatwebsite/excel

phplaravellaravel-5

提问by Geoff

I am using maatwebsite/excelto read data and I would like to skip the first two rows.

我正在使用maatwebsite/excel读取数据,我想跳过前两行。

Reading data:

读取数据:

$data = Excel::load('storage/app/temporary'.'/'.$request->input('file'))->get();
return $data;

The above code returns all the rows

上面的代码返回所有的行

How do I skip the first two rows?

如何跳过前两行?

采纳答案by Sohel0415

Or you could use limit()

或者你可以使用 limit()

$data = Excel::load('storage/app/temporary'.'/'.$request->input('file'))-->limit(false, 2)->get();
return $data;

Or try adding this line before read-

或者尝试在阅读之前添加这一行 -

config(['excel.import.startRow' = 2]);

this was answered here, you can see this link to have a better understanding.

这是在这里回答的,您可以查看此链接以更好地理解。

回答by Felippe Duarte

Use the skipRows()function:

使用skipRows()函数:

$data = Excel::load('storage/app/temporary'.'/'.$request->input('file'))->skipRows(2)->get();
return $data;

Or you can try with:

或者你可以尝试:

$file = 'storage/app/temporary'.'/'.$request->input('file');

$data = Excel::load($file, function($reader) {
    $results = $reader->skipRows(2)->get();
    return $results;
});

回答by Mizhar Raja

Add this code before load the Excel file. it works for me.

在加载 Excel 文件之前添加此代码。这个对我有用。

config(['excel.import.startRow' => your_number_of_row_to_skip]);

Ex:

前任:

$path = $request->file('uploaded_file')->getRealPath();
config(['excel.import.startRow' => 4]);
$data = Excel::load($path)->get();

回答by Sérgio Reis

Check this out https://laravel.com/docs/5.5/collections#method-slice

看看这个 https://laravel.com/docs/5.5/collections#method-slice

So for example,

例如,

return collect($data)->slice(2);