PHPExcel引发致命错误:耗尽的内存大小为134217728字节(试图分配71字节) [英] PHPExcel throws Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes)

查看:191
本文介绍了PHPExcel引发致命错误:耗尽的内存大小为134217728字节(试图分配71字节)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHPExcel(位于此处: https://github.com/PHPOffice/PHPExcel ) .如果我尝试读取约2000行以上的内容,则会显示内存错误,如下所示.

I am using PHPExcel (found here: https://github.com/PHPOffice/PHPExcel). If i try to read more than approximately 2000 rows then it shows memory error as follows.

严重错误:耗尽的134217728字节允许的内存大小(尝试 分配71个字节) /home/sample/PHPExcelReader/Classes/PHPExcel/worksheet.php,第89行

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes) in /home/sample/PHPExcelReader/Classes/PHPExcel/worksheet.php on line 89

我的Excel数据范围是A1:X2000

My Excel data range is A1:X2000

下面是我用来阅读Excel的代码.

ini_set('memory_limit', '-1');
/** Include path **/
        set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');


    /** PHPExcel_IOFactory */
    include $unsecured_param['home_dir'].'APIs/PHPExcelReader/Classes/PHPExcel/IOFactory.php';
    $inputFileName = $target;  // File to read
    //echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory to identify the format<br />';
    try {
        $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
    } catch(Exception $e) {
        die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
    }

    $sheetData = $objPHPExcel->getActiveSheet()->rangeToArray('A1:X2000', null, true, true, true)
    //store data into array..
    $i=0;$j=0;$max_rows=0;$max_columns=0;
    foreach($sheetData as $rec)
    {
        foreach($rec as $part)
        {//echo "items[$j][$i]=" ; echo $part;echo "<br>";
            $items[$j][$i]=$part; $i=$i+1;
            if($j==0) {$max_columns=$i;}
        }
        $j=$j+1;$i=0;
    }
    $max_rows=$j;

有人可以让我知道如何解决这个问题吗?

Could any one please let me know how to overcome this issue ?

推荐答案

请考虑使用单元缓存来减少将工作簿保存在内存中所需的内存,如开发人员文档的4.2.1节所述

Consider using cell caching to reduce the memory required to hold the workbook in memory, as described in section 4.2.1 of the developer documentation

并考虑使用toArray(),然后使用该函数在内存中构建另一个数组....这样做实际上是在使用大量内存来保存重复的数据,这时您可以简单地循环浏览工作表的行和列以完成所需的操作

And consider not using toArray() and then using that to build another array in memory.... doing this is really using a lot of memory to hold duplicated data, when you could simply loop through the rows and columns of the worksheet to do what you need

这篇关于PHPExcel引发致命错误:耗尽的内存大小为134217728字节(试图分配71字节)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆