我将尝试使用PHPExcel库在PHP(Laravel4)中导出大约40,000行的Mysql数据。
以下是我的代码:
($patList是结果列的数组)
($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列,且有15000行,则以上代码可以正常运行。但是,如果行数增加到30000或列数增加到10,则无法生成Excel文件。
ini_set('memory_limit', '1024M');
或者您认为足够脚本使用的内存大小。 - Bogdan