exporting large files to excel using PHPExcel

陌路散爱 提交于 2019-12-10 17:16:10

问题


I am trying to export around 40,000 rows of Mysql data in PHP(Laravel4) using PHPExcel library. Below is my code:

($patList is an array of result columns)

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');

The above code runs fine if there are 3-4 columns in the excel and 15,000 rows. However, if I increase the no. of rows to 30,000 or the no. of columns to 10, the excel doesn't get generated.


回答1:


$cacheSettings = array( 'memoryCacheSize' => -1);

isn't sensible.... I don't even know if using a value of -1 will work; but if it does, it will mean that you're storing everything in memory and nothing in php://temp

The memoryCacheSize value tells the cache stream how much data should be stored in memory before switching data out to php://temp ; so

$cacheSettings = array( 'memoryCacheSize' => '8MB');

would tell the cache stream to use 8MB of memory, and then if additional data needed to be stored to use php://temp instead



来源:https://stackoverflow.com/questions/26545538/exporting-large-files-to-excel-using-phpexcel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!