使用此代码,我可以将 MyModel 的数据导出到 Excel,工作正常:
Excel::create('Filename', function ($excel) {
$excel->sheet('Contratos', function ($sheet) {
$datos = MyModel::select([
'id', 'data'])
->get();
$sheet->fromArray($datos, null, 'A1', false, false);
});
})->export('xls');
但我需要对数据进行分块才能生成 Excel,因为在另一个模型中我有太多数据(例如 9000 条记录)。我看到过一些这样的例子:
http://www.thecreativeroad.com/blog/export-large-dataset-using-laravel-excel-using-laravel-db-chunk http://www.thecreativeroad.com/blog/export-large-dataset-using-laravel-excel-using-laravel-db-chunk
$data = $user->query();
try{
return Excel::create('FileName', function($excel) use($data){
$data->chunk(100, function ($users) use($excel) {
$collection = $this->transformCollection($users);
$excel->sheet('OrgSubUserList', function($sheet) use($collection){
$sheet->fromModel($collection, null, 'A1', true);
});
});
})->export('xls');
}
catch(Exception $e)
{
return false;
}
但我不知道如何将其应用到我的代码中,我对工作表上的顺序和文件本身之间存在混淆。有什么提示吗?