I am using phpexcel to write a xlsx file.
It is working fine except I am running out of memory.
This is because I am writing two sheets to the same file, both with 15k+ rows.
What I want to do is
create a single sheet and, save it to file, or sanitized somewhere so it doesnt take up much space.
create the next sheet.
deal with the output.
Is there any way to do this?
While PHPExcel is described as an "in memory" spreadsheet, and constrained by PHP's memory limit, it is possible to reduce the memory required by using cell caching. This technique is fully described in the developer documentation (section 4.2.1), and can be configured to compress the cell objects in memory, or cache the cell data to memory caches such as APC, Wincache or memcache, or to disk. It can reduce memory usage by as much as 60%, though at a cost in speed. Using cell caching might allow you to create both worksheets in the same workbook without running out of memory.
If you want to create two workbooks, each with a single worksheet, rather than one workbook with two worksheets, then you'll need to flush the first workbook from memory once you've saved it, before starting to build the second... otherwise you'll save very little memory. Because of the cyclic references within the workbook object, you'll need to break these before unsetting the PHPExcel object. This is described in the developer documentation section 4.3
Basically:
while(... have sheet 1 data...) {
build sheet 1
}
while(... have sheet 2 data...) {
build sheet 2
}
instead of
while (... have data...) {
add to sheet 1
add to sheet 2
}
If you can't hold the entire spreadsheet in memory, then you'll have to increase the memory limit. As far as I know, PHPExcel and the other spreadsheet building-libraries won't work with an on-disk representation, just what can fit in memory.