【发布时间】:2014-12-20 03:37:32
【问题描述】:
我正在尝试使用 PHPExcel 库在 PHP(Laravel4) 中导出大约 40,000 行 Mysql 数据。 以下是我的代码:
($patList 是一个结果列数组)
set_time_limit ( 3000 );
$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;
$cacheSettings = array( 'memoryCacheSize' => -1);
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
$objPHPExcel = new PHPExcel();
$i = 1;
$patList = $result[0];
for ($r = 0; $r < count($patList); $r++) {
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue("A$i", $patList[$r][0])
->setCellValue("B$i", $patList[$r][1])
->setCellValue("C$i", $patList[$r][2])
->setCellValue("D$i", $patList[$r][1])
->setCellValue("E$i", $patList[$r][2])
->setCellValue("F$i", $patList[$r][1])
->setCellValue("G$i", $patList[$r][2])
->setCellValue("H$i", $patList[$r][2])
->setCellValue("I$i", $patList[$r][1])
->setCellValue("J$i", $patList[$r][2])
->setCellValue("K$i", $patList[$r][5]);
$i++;
}
$objPHPExcel->getActiveSheet()->setTitle('Name of Sheet 1');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="result.xls"');
header('Cache-Control: max-age=0');
ob_clean();
flush();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
如果 excel 中有 3-4 列和 15,000 行,上述代码运行良好。但是,如果我增加编号。行数到 30,000 或没有。列到 10,不会生成 excel。
【问题讨论】:
-
您是否遇到任何错误?是否启用错误报告?确保您的脚本不会耗尽内存,因为您在内存中保留了相当大的变量。
-
尝试设置
ini_set('memory_limit', '1024M');或任何您认为足够内存用于脚本的内容。 -
是的,现在可以了!它消耗了 228 MB 内存,这就是它失败的原因。非常感谢:)