所以我使用大型excel文件,首先我从 PHP 开始,但我总是遇到内存大小问题,即使我增加了PHP内存限制我有其他人Apache的问题,我尝试了所有的事情,但总是同样的问题。
所以,如果有人知道如何处理大型excels文件,我将非常感激。
答案 0 :(得分:1)
请参考。这将帮助您阅读PHP中的Excel文件
可以在" chunks"中读取工作表。使用读取过滤器,请检查自己的工作
inputFileType = 'Excel5';
$inputFileName = './sampleData/example2.xls';
/** 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;
}
}
echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
/** 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 = 20;
/** 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 <= 240; $startRow += $chunkSize) {
echo 'Loading WorkSheet using configurable filter for headings row 1 and for rows ',$startRow,' to ',($startRow+$chunkSize-1),'<br />';
/** 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()->toArray(null,true,true,true);
var_dump($sheetData);
echo '<br /><br />';
}
请注意,此读取过滤器将始终读取工作表的第一行以及块规则定义的行。
使用读取过滤器时,PHPExcel仍会解析整个文件,但只加载与定义的读取过滤器匹配的单元格,因此它只使用该数量的单元格所需的内存。但是,它会多次解析文件,每个块一次,因此速度会慢一些。此示例一次读取20行:要逐行读取,只需将$ chunkSize设置为1。
如果您的公式引用了不同的&#34;块&#34;,这也会导致问题,因为数据根本不适用于当前&#34; chunk&#34;之外的单元格。