dongwo2222 2014-02-09 05:04
浏览 76
已采纳

致命错误:允许的内存大小为xxx字节耗尽Php excel

I have made a script and i am getting the following error. If have upgrade the memory size to 7g still the error. So i tried to append 10000 records and save it and go for the next 10000 to solve it, works, but still getting the error.

Is there any way to flush the workbook or other methods?

Thnx!

The awnser:

PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);   
$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_discISAM;
$cacheSettings = array( 
    'dir' => '/home/domains/xxxx.nl/public_html/xxxxx'
);



require_once 'PHPExcel.php';
require_once 'PHPExcel/Writer/Excel2007.php';
require_once 'PHPExcel/IOFactory.php';



$counter = $result->num_rows;//159920
$spreadsheet = new PHPExcel();
$spreadsheet->setActiveSheetIndex(0);
$worksheet = $spreadsheet->getActiveSheet();


//header
$worksheet->setCellValueExplicit('A' . 1, 'Filename', PHPExcel_Cell_DataType::TYPE_STRING);
etc
etc

$rownr = 2;
$counterN = 0;
while ($row = mysqli_fetch_assoc($result)) {  
    $worksheet->setCellValueExplicit('A' . $rownr, utf8_encode($row['filename']), PHPExcel_Cell_DataType::TYPE_STRING);
    etc
    etc

    if( $counterN == 10000 ){

        $objWriter = new PHPExcel_Writer_Excel2007($spreadsheet);
        $objWriter->save('test.xlsx');

        $objPHPExcel = PHPExcel_IOFactory::load("test.xlsx");
        $objPHPExcel->setActiveSheetIndex(0);
        $row = $objPHPExcel->getActiveSheet()->getHighestRow()+1;
        $counterN = 0;
    }
    $counterN++;

}

$objWriter = new PHPExcel_Writer_Excel2007($spreadsheet);
$objWriter->save('test.xlsx');
  • 写回答

1条回答 默认 最新

  • doushan2811 2014-02-09 11:22
    关注

    If you don't have enough memory to handle a large number of cells, then the recommended method of reducing memory usage is to use cell caching, as described in section 4.2.1. of the developer documentation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀