MYISAMエンジンを搭載したMySQLデータベーステーブルからLaravelの.xlsx
ファイルに大量のデータセットをエクスポートする必要があります。大きなデータをxlsxにエクスポート
私はパッケージを使用しています。これはPHPExcelに基づいています。
93,000列(約46,500,000セル)の約500,000行のデータコンシューマと、ヘッダー構造に関するかなりの計算があります。しかし、ファイルの作成自体は長い時間がかかり、
// $excel_data contains some data regarding the project, nothing relevant here
$output = Excel::create('myproject-' . $excel_data->project->name . '-'.date('Y-m-d H:i:s') . '-export', function($excel) use($excel_data) {
// Set the title
$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();
// my server can't handle a request that returns more than 20k rows so I am chunking the results in batches of 15000 to be on the safe side
$query->chunk(15000, function($taskmetas) use($sheet, &$rowPointer, $totalRecords) {
// Iterate over taskmetas
foreach ($taskmetas as $taskmeta) {
// other columns and header structure omitted for clarity
$sheet->setCellValue('A' . $rowPointer, $rowPointer);
$sheet->setCellValue('B' . $rowPointer, $taskmeta->id);
$sheet->setCellValue('C' . $rowPointer, $taskmeta->url);
// Move on to the next row
$rowPointer++;
}
// logging the progress of the export
activity()
->log("wrote taskmeta to row " . $rowPointer . "/" . $totalRecords);
unset($taskmetas);
});
});
});
$output->download('xlsx');
がログによると、行が正常にファイルに書き込まれます。
この
は私が現在使用しているコードです。実際には、それは1時間で完了しません(これはこの関数の最大実行時間です)。 CSVファイルへのエクスポートは素晴らしい作品、それがコンパイルされ、約10分ファイル&ダウンロードそれで、しかし私はそれで動作することはできません - xlsx
するを必要出力ファイルを。
ファイル作成プロセスを高速化するにはどうすればよいですか?私は同じ結果を達成できる限り、他の選択肢にもオープンしています。
、私はそれは、コードの20〜30行になります賭けます... Googleの周り。ヘックPythonでそれをすべて行う、 – Kyslik
はなぜCSVに固執し、作成した後のxlsxにそれを変換していない...私はPHPの男ですが、私はその長い実行中のジョブので、この場合にはPythonに依存しています。ここではいくつかのリソースがhttps://stackoverflow.com/questions/33815465/phpexcel-csv-to-xlsx https://www.phpclasses.org/browse/file/40495.htmlをXLSXするために、CSVからの変換のためのものです – Oluwaseye