从大型数据集中获取 OOM
Getting OOM from large dataset
我正在尝试从包含 60k 条目的数组创建电子表格 (XLSX)。当它导出时,它给了我一个电子表格,上面只有 PHP OOM 警告。
$spreadsheet = new Spreadsheet();
$spreadsheet->setActiveSheetIndex(0);
$activeSheet = $spreadsheet->getActiveSheet();
$rowIndex = 2;
foreach ($this->_values as $_val){
$activeSheet->setCellValueByColumnAndRow(1, $rowIndex, trim($_val['item1']));
$activeSheet->setCellValueByColumnAndRow(2, $rowIndex, trim($_val['item2']));
$activeSheet->setCellValueByColumnAndRow(3, $rowIndex, trim($_val['item3']));
$activeSheet->setCellValueByColumnAndRow(4, $rowIndex, trim($_val['item4']));
$activeSheet->setCellValueByColumnAndRow(5, $rowIndex, trim($_val['item5']));
$activeSheet->setCellValueByColumnAndRow(6, $rowIndex, trim($_val['item6']));
$activeSheet->setCellValueByColumnAndRow(7, $rowIndex, trim($_val['item7']));
$activeSheet->setCellValueByColumnAndRow(8, $rowIndex, trim($_val['item8']));
$activeSheet->setCellValueByColumnAndRow(9, $rowIndex, trim($_val['item9']));
$activeSheet->setCellValueByColumnAndRow(10, $rowIndex, trim($_val['item10']));
$activeSheet->setCellValueByColumnAndRow(11, $rowIndex, trim($_val['item11']));
$rowIndex += 1;
}
$spreadsheet->garbageCollect();
$writer = new Xlsx($spreadsheet);
$writer->setPreCalculateFormulas(false);
$writer->setUseDiskCaching(true);
$writer->save("php://temp");
$spreadsheet->disconnectWorksheets();
unset($spreadsheet);
内存不足错误是因为您的系统内存不足。你可以简单地 increase the amount of memory 使用像 ini_set('memory_limit', '750M');
假设你的系统有 space
或者,如果不需要 XLSX(即:如果您可以改用 CSV,Excel 仍然可以打开),您可以将数据流式传输到电子表格,而不是将其全部加载到内存中一次,然后打印。看起来像这样:
foreach ($this->_values as $_val){
for($i=1; $i<=11; $i++) {
echo trim($_val['item'.$i]);
if($i<11) echo ",";
}
echo "\n";
}
您也可以 include the CSV header at the top of the PHP file,这样页面会提示下载而不是显示 CSV 文件的内容。这是 CSV header 校准:header("Content-type: text/csv");
我正在尝试从包含 60k 条目的数组创建电子表格 (XLSX)。当它导出时,它给了我一个电子表格,上面只有 PHP OOM 警告。
$spreadsheet = new Spreadsheet();
$spreadsheet->setActiveSheetIndex(0);
$activeSheet = $spreadsheet->getActiveSheet();
$rowIndex = 2;
foreach ($this->_values as $_val){
$activeSheet->setCellValueByColumnAndRow(1, $rowIndex, trim($_val['item1']));
$activeSheet->setCellValueByColumnAndRow(2, $rowIndex, trim($_val['item2']));
$activeSheet->setCellValueByColumnAndRow(3, $rowIndex, trim($_val['item3']));
$activeSheet->setCellValueByColumnAndRow(4, $rowIndex, trim($_val['item4']));
$activeSheet->setCellValueByColumnAndRow(5, $rowIndex, trim($_val['item5']));
$activeSheet->setCellValueByColumnAndRow(6, $rowIndex, trim($_val['item6']));
$activeSheet->setCellValueByColumnAndRow(7, $rowIndex, trim($_val['item7']));
$activeSheet->setCellValueByColumnAndRow(8, $rowIndex, trim($_val['item8']));
$activeSheet->setCellValueByColumnAndRow(9, $rowIndex, trim($_val['item9']));
$activeSheet->setCellValueByColumnAndRow(10, $rowIndex, trim($_val['item10']));
$activeSheet->setCellValueByColumnAndRow(11, $rowIndex, trim($_val['item11']));
$rowIndex += 1;
}
$spreadsheet->garbageCollect();
$writer = new Xlsx($spreadsheet);
$writer->setPreCalculateFormulas(false);
$writer->setUseDiskCaching(true);
$writer->save("php://temp");
$spreadsheet->disconnectWorksheets();
unset($spreadsheet);
内存不足错误是因为您的系统内存不足。你可以简单地 increase the amount of memory 使用像 ini_set('memory_limit', '750M');
假设你的系统有 space
或者,如果不需要 XLSX(即:如果您可以改用 CSV,Excel 仍然可以打开),您可以将数据流式传输到电子表格,而不是将其全部加载到内存中一次,然后打印。看起来像这样:
foreach ($this->_values as $_val){
for($i=1; $i<=11; $i++) {
echo trim($_val['item'.$i]);
if($i<11) echo ",";
}
echo "\n";
}
您也可以 include the CSV header at the top of the PHP file,这样页面会提示下载而不是显示 CSV 文件的内容。这是 CSV header 校准:header("Content-type: text/csv");