简体   繁体   English

在phpexcel中一次写一张纸

[英]write one sheet at a time in phpexcel

I am using phpexcel to write a xlsx file. 我正在使用phpexcel编写一个xlsx文件。

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. 这是因为我正在将两张纸都写到同一个文件中,每张纸都有15k +行。

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? 有什么办法吗?

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. 据我所知,PHPExcel和其他电子表格构建库无法使用磁盘上的表示形式,而只能用于内存中。

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. 虽然PHPExcel被描述为“内存中”电子表格,并受PHP内存限制的约束,但可以通过使用单元缓存来减少所需的内存。 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. 此技术在开发人员文档(第4.2.1节)中有完整描述,可以配置为压缩内存中的单元对象,或将单元数据高速缓存到内存高速缓存(例如APC,Wincache或memcache)或磁盘中。 It can reduce memory usage by as much as 60%, though at a cost in speed. 它可以减少多达60%的内存使用量,但会降低速度。 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. 由于工作簿对象中存在循环引用,因此在取消设置PHPExcel对象之前,您需要先打破这些引用。 This is described in the developer documentation section 4.3 开发人员文档第4.3节对此进行了描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM