我有以下代码
<?php
ini_set('memory_limit','1600M');
ini_set('max_execution_time', 3000);
require("phpexcel/Classes/PHPExcel.php");
$inputFileName = 'testa.xlsx';
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
function convert($size)
{
$unit=array('b','kb','mb','gb','tb','pb');
return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
/** Define a Read Filter class implementing PHPExcel_Reader_IReadFilter */
class chunkReadFilter implements PHPExcel_Reader_IReadFilter
{
private $_startRow = 0;
private $_endRow = 0;
/** Set the list of rows that we want to read */
public function setRows($startRow, $chunkSize) {
$this->_startRow = $startRow;
$this->_endRow = $startRow + $chunkSize;
}
public function readCell($column, $row, $worksheetName = '') {
// Only read the heading row, and the rows that are configured in $this->_startRow and $this->_endRow
if (($row == 1) || ($row >= $this->_startRow && $row < $this->_endRow)){
return true;
}
return false;
}
}
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
echo '<hr />';
/** Define how many rows we want to read for each "chunk" **/
$chunkSize = 25;
/** Create a new Instance of our Read Filter **/
$chunkFilter = new chunkReadFilter();
/** Tell the Reader that we want to use the Read Filter that we've Instantiated **/
$objReader->setReadFilter($chunkFilter);
/** Loop to read our worksheet in "chunk size" blocks **/
/** $startRow is set to 2 initially because we always read the headings in row #1 **/
for ($startRow = 2; $startRow <= 100; $startRow += $chunkSize) {
/** Tell the Read Filter, the limits on which rows we want to read this iteration **/
$chunkFilter->setRows($startRow,$chunkSize);
/** Load only the rows that match our filter from $inputFileName to a PHPExcel Object **/
$objPHPExcel = $objReader->load($inputFileName);
// Do some processing here
$sheetData = $objPHPExcel->getActiveSheet();
$highestRow = $sheetData->getHighestRow();
//$sheetData = $sheetData->toArray(null,true,true,true);
//var_dump($sheetData);
echo '<br /><br />';
echo convert(memory_get_peak_usage(true));
}
?>
并且当运行时,它输出该响应。
277 mb
294.5 mb
295.5 mb
296.75 mb
它一次读取25行,以此类推。我想不通的是,为什么记忆峰值一直在上升?
我知道在处理整个Excel文件之前必须先读取它,但每次都应该使用相同的内存量,因此内存使用量不会随着时间的推移而发生很大变化。然而,它似乎一直在上升,我不明白为什么。
使用PHPExcel时,可以采取许多措施来保留较少的内存。在Apache中修改服务器的内存限制之前,我建议您采取以下操作来优化内存使用。
/* Use the setReadDataOnly(true);*/
$objReader->setReadDataOnly(true);
/*Load only Specific Sheets*/
$objReader->setLoadSheetsOnly( array("1", "6", "6-1", "6-2", "6-3", "6-4", "6-5", "6-6", "6-7", "6-8") );
/*Free memory when you are done with a file*/
$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);
避免使用非常大的Exel文件,记住是文件大小导致进程运行缓慢并崩溃。
避免使用getCalculatedValue();读取单元格时的功能。
即使您是按块读取数据,PHPExcel仍保留电子表格的内存表示。你读的数据越多,你需要的内存就越多。
将表示形式保存在内存中有助于在电子表格中的任何位置添加/编辑单元格,以及对行/列进行一些计算(例如,要调整列的宽度,您需要知道该列中每个非空单元格的宽度,并且将所有数据存储在内存中可以更容易地检索)。
一般来说,你读取的每个单元格都会占用1K的内存。您可以使用PHPExcel提供的不同缓存机制对此进行优化。虽然内存优化会带来性能损失,但这是一种权衡。
我遇到了一个类似的问题,我相信我已经找到了PHPExcel库的PHPExcel_Calculation类。在我的测试中,我看到它的$_workbookSets数组从未被清空,并且在每次块迭代中都会继续添加更多的实例。
不幸的是,我还没能找到确切的原因,但unsetInstance()方法似乎只在脚本执行的最后调用,即调用PHPException类析构函数时调用。
调用disconnectWorksheets()方法对修复此问题没有任何作用,也没有通过gc_collect_cycles()强制PHP的垃圾收集。
我的临时解决方案是向Calculation类添加一个新的unsetInstances()静态方法,该方法将$_workbookSets设置为一个空数组,然后在块循环结束时调用该方法。
在PHPExcel库的Calculation.php中:
public static function unsetInstances() {
self::$_workbookSets = array();
}
然后调用函数作为循环中的最后一行:
PHPExcel_Calculation::unsetInstances();