laravel 如何删除laravel excel中的空行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28699079/
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
how to delete empty rows in laravel excel?
提问by Mohammed Hassar
So i work with Laravel, and i use Laravel excel to load excel/csv files, but my files contains empty rows and i want to delete every empty row.
所以我使用 Laravel,我使用 Laravel excel 加载 excel/csv 文件,但我的文件包含空行,我想删除每个空行。
this is my code :
这是我的代码:
Excel::selectSheetsByIndex(0)->load($path, function($reader){
$results = $reader->noHeading()->toArray();
foreach ($results as $row) {
//my code
}
}, 'UTF-8');
So please if someone has any idea how i can do that i will be very appreciative
所以请如果有人知道我该怎么做,我将非常感激
回答by Crowlex
I think you can do it in this way
我认为你可以这样做
/** @var LaravelExcelReader $data */
$data = \Excel::load('file.xls', function ($reader) {
$reader->limitRows(20);
$reader->ignoreEmpty();
})->get()->toArray();
# remove empty rows
$data = array_filter($data);
回答by OtienoJulie
use ToCollection method the wrap everything within if($row->filter()->isNotEmpty())
使用 ToCollection 方法包装其中的所有内容 if($row->filter()->isNotEmpty())
public function collection(Collection $rows)
{
foreach($rows as $row) {
if($row->filter()->isNotEmpty()){
// you logic can go here
$user = User::create([
'name' => ucwords($row['name']),
'class' => $row['class'],
...
]);
}
}
}