PHPExcel内存问题

前端之家收集整理的这篇文章主要介绍了PHPExcel内存问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试遍历3mb的Excel文档,以获取我将必须插入数据库的所有数据.我正在使用的工作表有6500行,但将来可能会有所不同.我注意到即使我使用推荐的内存保存技术,它仍然会绊倒
$reader = PHPExcel_IOFactory::createReaderForFile($file_path);
$reader->setReadDataOnly(true);

//$sheets = $this->getWorksheetNames($file['tmp_name'],0);
$reader->setLoadSheetsOnly('spreadsheetname');

$chunkFilter = new IPO_Reader(); 
$reader->setReadFilter($chunkFilter); 

$highestRow    = 10000; //$this->objWorksheet->getHighestRow();
$chunkSize     = 1; 
$highestColumn = "Y";

for ($startRow = 2; $startRow <= $highestRow; $startRow += $chunkSize) 
{ 

    $chunkFilter->setRows($startRow,$chunkSize); 
    $objPHPExcel  = $reader->load($file_path); 

    for($row = $startRow ; $row <= $startRow + $chunkSize; $row++)
    {
        $this->read_row = $objPHPExcel->getActiveSheet()->rangeToArray('A'.$row.':'.$highestColumn.$row,null,true,true);

        $this->read_row = end($this->read_row);         

        foreach($this->read_row as $column => $value)
        {
            $db_column_name = $this->_getDbColumnMap($column);
            if(!empty($db_column_name))
            {
                $this->new_data_row[$db_column_name] = $this->_getRowData($value,$column);
            }   

        }

        $this->read_row = null;
        $this->new_data_row['date_uploaded']    = date("Y-m-d H:i:s");
        $this->new_data_row['source_file_name'] = $file_name;
        $ipo_row  = new Model_UploadData_IPO();
        $ipo_row->create($this->new_data_row);
        $this->new_data_row = null;
        unset($ipo_row);

        gc_collect_cycles();

    }
    $objPHPExcel->disconnectWorksheets(); 
    unset($objPHPExcel);    
    gc_collect_cycles();

当我在取消设置objPHPExcel之前测试内存使用情况之后,没有内存增益,我真的很困惑,因为拆分成块似乎不允许我在每个块之后清除内存,以及用法逐渐上升,限制设置为250MB,它只允许我添加约500条记录

PHP excel库 is known to have these memory issues,我也有问题.对我有用的是这个建议(从上面的链接,尝试它,有很好的建议如何减少内存使用):
$objReader = new PHPExcel_Reader_Excel5();
$objReader->setReadDataOnly(true); /* this */

但无论如何,内存需求很大,因为它们为每个单元分配了大量内存(用于格式化等,即使不需要).在他们发布新版本的库之前,我担心我们会无助.

猜你在找的PHP相关文章