我尝试使用phpExcel将数据写入带有百万条记录的excel。但这需要太多时间。
$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);
我该怎么做?
答案 0 :(得分:0)
如果您使用的是PHPExcel,请查看:https://stackoverflow.com/a/5984286/4499987。 Mark谈到了一些优化PHPExcel的方法。你可以从很多其他帖子中找到帮助。
如果你真的关心速度,我还建议你看看Spout:https://github.com/box/spout。它使用起来非常简单,不需要任何优化,可以帮助您立即创建Excel文件!