Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a million records into an Excel

I tried to write data into excel with a million record using phpExcel. but it take too much time.

 

    $header=array('test1','test1','test1','test1','test1','test1','test1','test1');
    
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw") ->setLastModifiedBy("Maarten Balliauw") ->setTitle("Office 2007 XLSX Test Document") ->setSubject("Office 2007 XLSX Test Document") ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") ->setKeywords("office 2007 openxml php") ->setCategory("Test result file");
$objPHPExcel->getActiveSheet()->fromArray($header,NULL,'A1');
$sheet = $objPHPExcel->getActiveSheet();
// $final_xls_data1 is set of small records // $rowcount this variable is set of old rowcounts and set
$rowcount=$rowcount +1.
// create 8 small set of records. then add array in excel object $sheet->fromArray($final_xls_data1,NULL,'A'.$rowcount); $objPHPExcel->getActiveSheet()->setTitle('Simple'); $objPHPExcel->setActiveSheetIndex(0); header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="01simple.xlsx"'); header('Cache-Control: max-age=0'); header('Cache-Control: max-age=1'); header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header ('Pragma: public'); // HTTP/1.0 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save($xls_file_path);

How can I do this?

like image 624
pmali Avatar asked Dec 11 '25 10:12

pmali


1 Answers

If you are using PHPExcel, look at this: https://stackoverflow.com/a/5984286/4499987. Mark talks about a bunch of ways to optimize PHPExcel. There are a lot of other posts that you can find help from.

If you really care about speed, I'd also recommend you to take a look at Spout: https://github.com/box/spout. It is super simple to use, does not require any optimizations and should help you create your Excel file in no time!

like image 72
Adrien Avatar answered Dec 12 '25 23:12

Adrien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!