Chris
Chris

Reputation: 58302

PHPExcel - Write/append in chunks

I am using the PHPExcel framework to try to write out a very large excel document from a mysql query.

Everything works fine until I hit the 5000 row mark (or there about's) where the page gets flagged with the error:

Fatal error: Allowed memory size of xxx bytes exhausted (tried to allocate yyy bytes) in zzz on line aaa

I know this is documented, and I have adjusted the memory allocation on the server but even so I am still hitting the ceiling. I have also tried to turn off formatting, but in reality I will need it on.

So is there a way to write in small chunks, or append to the excel document so I don't exhaust the memory allocation? I am thinking along the lines of the page writing say 1000 lines, then redirect to itself and process the next 1000 using a GET to keep track. For example:

index.php?p=0

then redirect to

index.php?p=1000,

But I can't find a way to append to an existing document without opening up the whole thing.

Upvotes: 4

Views: 5651

Answers (1)

Mark Baker
Mark Baker

Reputation: 212522

There is no way of writing in chunks, although a common mistake is for people to load their mysql data to an array, then loop through the array setting the Excel cell data. It's more memory efficient to set the cell data as you loop through the mySQL query resultset.

If you need to keep memory usage to a minimum, what cell caching method are you using? Cell caching is slower, but can save significant amounts of memory.

Upvotes: 3

Related Questions