PHPExcel导出300多列 - 占CPU的50%

时间:2015-07-16 12:45:13

标签: php phpexcel

我正在尝试从大约300列,大约12100行的表中导出数据。当我尝试使用PHPExcel导出时,它会永远加载,同时进程HTTPD.EXE(APACHE)占用50%的CPU并最终在大约30分钟后失败。

我已经研究并提出了所有改进技巧,但仍然没有区别。

从数据库中提取数据的查询需要0.3180秒,因此我知道问题不在于此问题,问题是写入Excel。

以下是我的代码片段

$objPHPExcel = new PHPExcel();



                $sql = "SELECT * from LAPDATATABLE order by file_modified";
                $lrs = CDB::ExecuteQuery($sql);
                $i = 2;
                $objPHPExcel->setActiveSheetIndex(0);

                $exceldata = $objPHPExcel->getActiveSheet();


                $exceldata->setCellValue("A" . 1 , "ITEM" );
                $exceldata->setCellValue("B" . 1 , "attachment" );
                $exceldata->setCellValue("C" . 1 , "ITEM_DETAIL" );
....
....
....



                while ($rows = CDB::GetAssoc($lrs)) 
                {

                    $exceldata->setCellValue("A" . $i , $rows['ITEM']);
                    $exceldata->setCellValue("B" . $i , $rows['attachment']);
                    $exceldata->setCellValue("C" . $i , $rows['ITEM_DETAIL']);
...
...
...

                      $i++;
                 }


                // Rename worksheet
                $objPHPExcel->getActiveSheet()->setTitle('LAP-TAB-DATA');


                // Set active sheet index to the first sheet, so Excel opens this as the first sheet
                $objPHPExcel->setActiveSheetIndex(0);


                // Redirect output to a client’s web browser (Excel5)
                header('Content-Type: application/vnd.ms-excel');
                header('Content-Disposition: attachment;filename="Raw-Lap-Data.xlsx"');
                header('Cache-Control: max-age=0');
                // If you're serving to IE 9, then the following may be needed
                header('Cache-Control: max-age=1');

                // If you're serving to IE over SSL, then the following may be needed
                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('php://output');
                exit;

也许有人可以在我的代码中看到错误,或者可以建议这就是编写这么多列的方式吗?

由于

1 个答案:

答案 0 :(得分:0)

或者,您可以尝试另一个名为Spout的开源库:https://github.com/box/spout。它非常擅长处理大型数据集,并且没有内存或性能问题!