9

I am using phpexcel to write quite a large excel file.

I am writing it from an array that looks like

array(
  [0] => stdClass Object
        (
            [sent] => Mar 31st, 2011 02:10PM
            [to_number] => 64211111111
            [additional_txt] => 
            [misc_data] => 
        )
  ...
  [14058] => stdClass Object
        (
            [sent] => Mar 31st, 2011 02:10PM
            [to_number] => 64211111111
            [additional_txt] => 
            [misc_data] => 
        )
)

The loop is

$r = 0;
foreach ($replies_obj as $row) {
  $c = 'A';
  foreach ($row as $col)
    $xlsx->getActiveSheet()->setCellValue($c++ . $r, $col);
  $r++;
}

and the error is

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 105 bytes) in /home/webspace/xxxx/test/htdocs/application/libraries/PHPExcel/Worksheet.php on line 961

Is this a phpexcel problem or am I doing something wrong?

How can I fix this?

Hailwood
  • 89,623
  • 107
  • 270
  • 423
  • yes/no worst case, but for obvious reasons I would like to avoid that... – Hailwood Apr 06 '11 at 00:14
  • 1
    also, I dont think you can split it into chunks as phpexcel seems to hold the workbook in memory, so unless it supports "segmented" writing, then that wont work – Hailwood Apr 06 '11 at 00:15
  • why avoid it? if it works, and you have the memory, use it. I don't know the class, so it was just a guess that you may be able to chunk it. –  Apr 06 '11 at 00:16

1 Answers1

13

There's a lot been written about PHPExcel and memory use, and I'm not going to repeat it all here.

Try reading some of the threads on the PHPExcel discussion board discussing the issue, such as this one; or previous answers here on SO such as this one or this one

Community
  • 1
  • 1
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • 2
    all those links are about reading, this is creating and offer for download to client. I have the same error on line 961 – Grumpy Jun 01 '12 at 22:57
  • 1
    @Grumpy - cell cacheing reduces memory usage whether you're reading or creating workbooks. Other threads on the forum discuss the use of setting seceral cells in a single call using fromArray() or setting styles for ranges of cells rather than each individual cell – Mark Baker Jun 01 '12 at 23:04
  • 1
    I ran into this problem and unfortunately none of the suggested solutions could help me. What I eventually did was writing each worksheet to an individual (temporary) file, and then combining these separate files with some special software I wrote. This reduced my memory consumption from >512 Mb to well under 100 Mb. See https://github.com/infostreams/excel-merge if you have the same problem. – Edward Feb 15 '16 at 11:46