我需要使用MYISAM引擎将MySQL数据库表中的一个巨大数据集导出到
.xlsx
在Laravel归档。
maatwebsite/laravel-excel
PHPExcel
数据集包含约500000行93列(约46500000个单元格),以及许多关于标头结构的计算。
这是我目前使用的代码:
$output = Excel::create('myproject-' . $excel_data->project->name . '-'.date('Y-m-d H:i:s') . '-export', function($excel) use($excel_data) {
$excel->setTitle($excel_data->project->name . ' Export');
$excel->sheet('Data', function($sheet) use($excel_data) {
$rowPointer = 1;
$query = DB::table('task_metas')
->where([
['project_id', '=', $excel_data->project->id],
['deleted_at', '=', null]
])
->orderBy('id');
$totalRecords = $query->count();
$query->chunk(15000, function($taskmetas) use($sheet, &$rowPointer, $totalRecords) {
foreach ($taskmetas as $taskmeta) {
$sheet->setCellValue('A' . $rowPointer, $rowPointer);
$sheet->setCellValue('B' . $rowPointer, $taskmeta->id);
$sheet->setCellValue('C' . $rowPointer, $taskmeta->url);
$rowPointer++;
}
activity()
->log("wrote taskmeta to row " . $rowPointer . "/" . $totalRecords);
unset($taskmetas);
});
});
});
$output->download('xlsx');
根据日志,行已成功写入文件,但文件创建本身需要很长时间。事实上,它并没有在1小时内完成(这是该函数的最大执行时间)。
将其导出到csv效果很好,只需大约10分钟即可编译文件;下载它,但是我不能使用它-输出文件
成为
xlsx
.
如何加快文件创建过程?只要我能达到同样的结果,我也愿意接受其他选择。