问题
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