使用maatwebsite / excel读取数据时,Laravel excell会跳过行

时间:2018-01-22 17:46:26

标签: php laravel laravel-5

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

阅读数据:

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

上面的代码返回所有行

如何跳过前两行?

4 个答案:

答案 0 :(得分:1)

使用skipRows()功能:

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

或者您可以尝试:

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

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

答案 1 :(得分:1)

或者您可以使用limit()

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

或尝试在阅读之前添加此行 -

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

已回答here,您可以看到此链接以便更好地理解。

答案 2 :(得分:0)

检查一下 https://laravel.com/docs/5.5/collections#method-slice

例如,

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

答案 3 :(得分:0)

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

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

例如:

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