【发布时间】:2015-06-14 23:57:21
【问题描述】:
我有以下代码
<?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 文件,但肯定每次都应该使用相同数量的内存,因此内存使用量不会随着时间的推移而发生很大变化。然而,它似乎在不断上升,我不知道是为什么。
【问题讨论】:
-
一个单元格可能包含一个数字,可以很容易地表示为一个固定长度的浮点数或整数值,另一个单元格可能包含一个 200 个字符的字符串
-
因为一个单元格可能有很多样式信息,另一个单元格只有默认样式信息
-
因为当您读取新块时,前一个块读取的样式不会从内存中刷新,这将读取该块中单元格使用的其他样式