如何load/create Excel 文件?

how to load/create Excel files?

所以我使用大excel文件,首先我从PHP开始,但我总是即使我增加了 PHP 内存限制,内存大小也有问题我在 Apache 上遇到了其他问题,我尝试了所有方法,但总是出现同样的问题。

因此,如果有人知道如何处理 excels 大文件,我将不胜感激。

请参阅此内容。这将帮助您阅读 PHP

中的 excel 个文件

Excel IO Factory

可以使用阅读过滤器阅读 "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.

如果您有引用不同 "chunks" 中的单元格的公式,这也会导致问题,因为数据根本无法用于当前 "chunk".

之外的单元格