PHPExcel reading too slow

不打扰是莪最后的温柔 提交于 2020-01-14 18:56:19

问题


I know, there are lot's of questions here sbout improving PHPExcel performance. But all of them are about writing data, and my problem is in reading.

My function:

function parse($filename){
    $objPHPExcel = PHPExcel_IOFactory::load($filename); 
    $activeSheet = $objPHPExcel->getActiveSheet();
    $parsedData = array();
    $columnHeaders = array('order', 'ts', 'summ', 'name', 'quant', 'price', 'bccu');
    foreach ($activeSheet->getRowIterator() as $rkey => $row) {
        $cellIterator = $row->getCellIterator();
        foreach ($cellIterator  as $ckey => $cell) {
            $parsedData[$columnHeaders[$ckey]] = $cell->getCalculatedValue();
        }
    }
    return $parsedData;
}

The file contains ~300 rows and 7 columns. And this script fails to run in 30 seconds.

How can i improve it?

edit:

used

$objReader = PHPExcel_IOFactory::createReader("Excel2007");
$objPHPExcel = $objReader->load($filename); 

whth no success


回答1:


If your columns are already defined, what about remove the column iterator?

Try something like this:

foreach ($activeSheet->getRowIterator() as $rkey => $row) {
    $rowIndex = $row->getRowIndex ();
    $parsedData[$rowIndex]['order'] = $activeSheet->getCell('A' . $rowIndex);
    $parsedData[$rowIndex]['ts']    = $activeSheet->getCell('B' . $rowIndex);
    $parsedData[$rowIndex]['summ']  = $activeSheet->getCell('C' . $rowIndex);
    .
    .
    .
}



回答2:


Try disabling the garbage collector before running parse() by issuing gc_disable(). Guessing that levels of iterations here don't get optimized properly by PHP.




回答3:


If you're not going to change the file's contents; setting the reader to read-only gives about 10x boost.

For example:

$objReader = PHPExcel_IOFactory::createReader( 'Excel5' );
$objReader->setReadDataOnly( true );


来源:https://stackoverflow.com/questions/10960364/phpexcel-reading-too-slow

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