When I try to generate big file with PHPExcel I get this error:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 67 bytes)
here's example code which causes this:
$CI = & get_instance ();
$CI->load->library ( 'export/PHPExcel' );
$objPHPExcel = new PHPExcel ();
$objPHPExcel->setActiveSheetIndex ( 0 );
$rowCount = 1;
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount,'title'.$rowCount);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount,'title'.$rowCount);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount,'title'.$rowCount);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount,'title'.$rowCount);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowCount,'title'.$rowCount);
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$rowCount,'title'.$rowCount);
for($i=0; $i<1000000; $i++){
$rowCount++;
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, "xyz".$rowCount);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, "xyz".$rowCount);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, "xyz".$rowCount);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount, "xyz".$rowCount);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowCount, "xyz".$rowCount);
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$rowCount, "xyz".$rowCount);
}
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="your_name.xls"');
header('Cache-Control: max-age=0');
$objWriter = new PHPExcel_Writer_Excel2007 ( $objPHPExcel );
$objWriter->save ( 'php://output' );
Now my question is how to avoid this issue, when I want to generate big xlsx files with PHPExcel? Or maybe you know better lirary for that? I can't change apache/php config to have more memory avaliable. Solution shouldn't depend on memory.
By default PHPExcel uses memory for cacheing before you save the result to file or php://output. You can switch to using php_temp, which is slower, but cheaper in terms of RAM.
Make sure to set settings before creating an instance of PHPExcel:
$cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
$cacheSettings = ['memoryCacheSize' => '128MB'];
\PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
$excelObject = new \PHPExcel();
Threre are also other values available for cacheMethod, such as MemCache, APC, ISAM, though I haven't tried them. You can see them in the docs of \\PHPExcel_CachedObjectStorageFactory.
Hope it helps. Good luck!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.