php导出excel数据大无法导出,PHPExcel导入导出大文件报500错误的官方解决方案-程序员宅基地

技术标签: php导出excel数据大无法导出  

PHP导出excel想必很多童鞋都碰到了,使用phpexcel类也确实方便,但导出大量数据的时候就没那么简单了,常常会伴随一些超时或内存溢出的问题,下面就给大家介绍一些方法,文章由原作者整理,出处PHPExcel导出大量数据超时及内存错误解决方法

PHPExcel是一个很强大的处理Excel的PHP开源类,但是很大的一个问题就是它占用内存太大,从1.7.3开始,它支持设置cell的缓存方式,但是推荐使用目前稳定的版本1.7.6,因为之前的版本都会不同程度的存在bug,以下是其官方文档:

PHPExcel1.7.6官方文档 写道

PHPExcel uses an average of about 1k/cell in your worksheets, so large workbooks can quickly use up  available memory. Cell caching provides a mechanism that allows PHPExcel to maintain the cell objects in a smaller size of memory, on disk, or in APC, memcache or Wincache, rather than in PHP memory. This allows you to reduce the memory usage for large workbooks, although at a cost of speed to access cell data.

PHPExcel 平均下来使用1k/单元格的内存,因此大的文档会导致内存消耗的也很快。单元格缓存机制能够允许PHPExcel将内存中的小的单元格对象缓存在磁盘或者 APC,memcache或者Wincache中,尽管会在读取数据上消耗一些时间,但是能够帮助你降低内存的消耗。

PHPExcel1.76.官方文档 写道

By default, PHPExcel still holds all cell objects in memory, but you can specify alternatives. To enable cell caching, you must call the PHPExcel_Settings::setCacheStorageMethod() method, passing in the caching method  that you wish to use.

默认情况下,PHPExcel依然将单元格对象保存在内存中,但是你可以自定义。你可以使用PHPExcel_Settings::setCacheStorageMethod()方法,将缓存方式作为参数传递给这个方法来设置缓存的方式。

Php代码:

$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory;

PHPExcel_Settings::setCacheStorageMethod($cacheMethod);

PHPExcel1.7.6官方文档 写道

setCacheStorageMethod() will return a boolean true on success, false on failure (for example if trying to    cache to APC when APC is not enabled).

setCacheStorageMethod()方法会返回一个BOOL型变量用于表示是否成功设置(比如,如果APC不能使用的时候,你设置使用APC缓存,将会返回false)

PHPExcel1.7.6官方文档 写道

A separate cache is maintained for each individual worksheet, and is automatically created when the    worksheet is instantiated based on the caching method and settings that you have configured. You cannot    change the configuration settings once you have started to read a workbook, or have created your first    worksheet.

每一个worksheet都会有一个独立的缓存,当一个worksheet实例化时,就会根据设置或配置的缓存方式来自动创建。一旦你开始读取一个文件或者你已经创建了第一个worksheet,就不能在改变缓存的方式了。

PHPExcel1.7.6官方文档 写道

Currently, the following caching methods are available.

目前,有以下几种缓存方式可以使用:

Php代码:

PHPExcel_CachedObjectStorageFactory::cache_in_memory;

PHPExcel1.7.6官方文档 写道

The default. If you don’t initialise any caching method, then this is the method that PHPExcel will use. Cell    objects are maintained in PHP memory as at present.

默认情况下,如果你不初始化任何缓存方式,PHPExcel将使用内存缓存的方式。

===============================================

Php代码:

PHPExcel_CachedObjectStorageFactory::cache_in_memory_serialized;

PHPExcle1.7.6官方文档 写道

Using this caching method, cells are held in PHP memory as an array of serialized objects, which reduces the    memory footprint with minimal performance overhead.

使用这种缓存方式,单元格会以序列化的方式保存在内存中,这是降低内存使用率性能比较高的一种方案。

===============================================

Php代码:

PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;

PHPExcel1.7.6官方文档 写道

Like cache_in_memory_serialized, this method holds cells in PHP memory as an array of serialized objects,   but- gzipped to reduce the memory usage still further, although access to read or write a cell is slightly slower.

与序列化的方式类似,这种方法在序列化之后,又进行gzip压缩之后再放入内存中,这会更进一步降低内存的使用,但是读取和写入时会有一些慢。

===========================================================

Php代码:

PHPExcel_CachedObjectStorageFactory::cache_to_discISAM;

PHPExcel1.7.6官方文档 写道

When using cache_to_discISAM all cells are held in a temporary disk file, with only an index to their location   in that file maintained in PHP memory. This is slower than any of the cache_in_memory methods, but significantly       reduces the memory footprint.The temporary disk file is automatically deleted when your script terminates.

当使用cache_to_discISAM这种方式时,所有的单元格将会保存在一个临时的磁盘文件中,只把他们的在文件中的位置保存在PHP的内存中,这会比任何一种缓存在内存中的方式都慢,但是能显著的降低内存的使用。临时磁盘文件在脚本运行结束是会自动删除。

===========================================================

Php代码:

PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;

PHPExcel1.7.6官方文档 写道

Like cache_to_discISAM, when using cache_to_phpTemp all cells are held in the php://temp I/O stream, with  only an index to their location maintained in PHP memory. In PHP, the php://memory wrapper stores data in the  memory: php://temp behaves similarly, but uses a temporary file for storing the data when a certain memory   limit is reached. The default is 1 MB, but you can change this when initialising cache_to_phpTemp.

类 似cache_to_discISAM这种方式,使用cache_to_phpTemp时,所有的单元格会还存在php://temp I/O流中,只把 他们的位置保存在PHP的内存中。PHP的php://memory包裹器将数据保存在内存中,php://temp的行为类似,但是当存储的数据大小超 过内存限制时,会将数据保存在临时文件中,默认的大小是1MB,但是你可以在初始化时修改它:

Php代码:

$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;

$cacheSettings = array(’memoryCacheSize’ => ’8MB’ );

PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);

PHPExcel1.7.6官方文档 写道

The php://temp file is automatically deleted when your script terminates.

php://temp文件在脚本结束是会自动删除。

===========================================================

Php代码:

PHPExcel_CachedObjectStorageFactory::cache_to_apc;

PHPExcle1.7.6官方文档 写道

When using cache_to_apc, cell objects are maintained in APC with only an index maintained in PHP memory   to identify that the cell exists. By default, an APC cache timeout of 600 seconds is used, which should be enough  for most applications: although it is possible to change this when initialising cache_to_APC.

当使用cach_to_apc时,单元格保存在APC中,只在内存中保存索引。APC缓存默认超时时间时600秒,对绝大多数应用是足够了,当然你也可以在初始化时进行修改:

Php代码:

$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_APC;

$cacheSettings = array(’cacheTime’ => 600 );

PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);

PHPExcel1.7.6官方文档 写道

When your script terminates all entries will be cleared from APC, regardless of the cacheTime value, so it   cannot be used for persistent storage using this mechanism.

当脚本运行结束时,所有的数据都会从APC中清除(忽略缓存时间),不能使用此机制作为持久缓存。

===========================================================

Php代码:

PHPExcel_CachedObjectStorageFactory::cache_to_memcache

PHPExcel1.7.6官方文档 写道

When using cache_to_memcache, cell objects are maintained in memcache with only an index maintained in  PHP memory to identify that the cell exists.

By default, PHPExcel looks for a memcache server on localhost at port 11211. It also sets a memcache  timeout limit of 600 seconds. If you are running memcache on a different server or port, then you can change   these defaults when you initialise cache_to_memcache:

使 用cache_to_memory时,单元格对象保存在memcache中,只在内存中保存索引。默认情况下,PHPExcel会在localhost和 端口11211寻找memcache服务,超时时间600秒,如果你在其他服务器或其他端口运行memcache服务,可以在初始化时进行修改:

Php代码:

$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_memcache;

$cacheSettings = array( ’memcacheServer’  => ’localhost’,

‘memcachePort’    => 11211,

‘cacheTime’       => 600

);

PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);

从初始化设置的形式上看,MS还不支持多台memcache服务器轮询的方式,比较遗憾。

PHPExcel1.7.6官方文档 写道

When your script terminates all entries will be cleared from memcache, regardless of the cacheTime value, so   it cannot be used for persistent storage using this mechanism.

当脚本结束时,所有的数据都会从memcache清空(忽略缓存时间),不能使用该机制进行持久存储。

===========================================================

Php代码:

PHPExcel_CachedObjectStorageFactory::cache_to_wincache;

PHPExcel1.7.6官方文档 写道

When using cache_to_wincache, cell objects are maintained in Wincache with only an index maintained in   PHP memory to identify that the cell exists. By default, a Wincache cache timeout of 600 seconds is used, which   should be enough for most applications: although it is possible to change this when initialising cache_to_wincache.

使用cache_towincache方式,单元格对象会保存在Wincache中,只在内存中保存索引,默认情况下Wincache过期时间为600秒,对绝大多数应用是足够了,当然也可以在初始化时修改:

Php代码:

$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_wincache;

$cacheSettings = array(’cacheTime’ => 600);

PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);

PHPExcel官方文档1.7.6 写道

When your script terminates all entries will be cleared from Wincache, regardless of the cacheTime value,  so  it cannot be used for persistent storage using this mechanism.

PHPExcel还是比较强大的,最大的问题就是内存占用的问题,PHPExcel啥时候能出一个轻量级的版本,不需要那么多花哨的功能,只需要导出最普通的数据的版本就好了!

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_29531177/article/details/115203921

智能推荐

Kafka 使用SASL / SCRAM进行身份验证-程序员宅基地

文章浏览阅读3.3k次。使用SASL / SCRAM进行身份验证 请先在不配置任何身份验证的情况下启动Kafka 1. 创建SCRAM Credentials 1.1 创建broker通信用户(或称超级用户) bash Emacs bin/kafka-configs.sh --zookeeper c..._kafka 验证sasl是否生效

android代码svn需要忽略的文件_svn需要屏蔽build.gradle文件吗-程序员宅基地

文章浏览阅读317次。一般需要忽略.idea文件夹、.gradle文件夹、所有的build文件夹、所有的.iml文件及local.properties文件,当然build.gradle、setting.gradle是不可以忽略的,我们忽略的这些文件夹都在项目编译过程中可以自动为我们生成的_svn需要屏蔽build.gradle文件吗

若依springcloud 前后端分离 集成 websocket_springcloud集成websocket-程序员宅基地

文章浏览阅读4.2k次,点赞5次,收藏24次。添加websocket 依赖 <!--websocket--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency>网管配置服务转_springcloud集成websocket

成功解决SyntaxError: future feature annotations is not defined-程序员宅基地

文章浏览阅读1.5w次,点赞9次,收藏13次。​成功解决SyntaxError:futurefeatureannotationsisnotdefined目录解决问题解决思路解决方法解决问题SyntaxError:futurefeatureannotationsisnotdefined解决思路语法错误:没有定义futurefeature注释解决方法实际上是python版本不对,该语法需要在python3.7以上,而当前版本在python3.6所以导致此错误!T1、Anaconda下安装_syntaxerror: future feature annotations is not defined

js控制输入框禁止输入和允许输入_js 某个input框的enter 其他input禁止-程序员宅基地

文章浏览阅读8.5k次。初始,部分输入框禁止输入:$(document).ready(function(){ $("#fundCode").prop("disabled",true); $("#convertFundCode").prop("disabled",true); $("#allowtradeVol").prop("disabled",true); $("#appVol")._js 某个input框的enter 其他input禁止

哈夫曼树数据压缩c语言算法,特殊的树:哈夫曼树2-程序员宅基地

文章浏览阅读143次。1.哈夫曼树的查找算法查找算法根据构建哈夫曼树算法衍生而来,我们在构建二叉树时需要查找出哪些数据最小,以符合我们哈夫曼树的最优解情况。查找权重值最小的两个结点的思想是:从待处理数据的头部位置开始,首先找到两个无父结点的结点(说明还未使用其构建成树),然后和后续无父结点的结点依次做比较,有两种情况需要考虑:l 如果比两个结点中较小的那个还小,就保留这个结点,删除原来较大的结点;l 如果介于两个结..._数据压缩赫夫曼树c语言

随便推点

格力YB0F2红外遥控编码分析_逻辑分析仪分析格力空调码选什么-程序员宅基地

文章浏览阅读2.6k次,点赞3次,收藏18次。格力YB0F2红外遥控编码分析逻辑分析仪捕捉到的脉冲图是这样的,与常见的NEC协议不同的是每组数据有8个字节,前4个字节和后4个字节中间有一个连接码引导码:9000us+4500us数据:1组前4字节数据连接码:3bit数据010 + 20000us高电平组成,数据:1组后4字节数据空闲:40ms高电平接着按上面的方法发送第2组数据逻辑0:600us+600us逻辑1:600us+1600us注意:下面所有提到的字节顺序与bit顺序均是从0开始注意数据方向,先收到了数据放在低位将会得_逻辑分析仪分析格力空调码选什么

ACM常用模板——数据结构——优先队列(堆)_acm优先队列模板-程序员宅基地

文章浏览阅读515次。数据结构——优先队列(堆)_acm优先队列模板

Android禁止安装三方应用(屏蔽所有安装应用的方式)_禁用手动安转-程序员宅基地

文章浏览阅读2.2w次,点赞5次,收藏48次。android系统安装应用有四种方式1.系统应用安装――开机时完成,没有安装界面2.网络下载应用安装――通过market应用完成,没有安装界面3.ADB工具安装――没有安装界面。4.第三方应用安装――通过现有的APK文件安装,有安装界面,由packageinstaller.apk应用处理安装及卸载过程的界面。1.禁止ADB和应用商店安装由“frameworks\base\services\core\java\com\android\server\pm\Packag..._禁用手动安转

CentOS 7输入startx无法启动图形化界面_centos startx-程序员宅基地

文章浏览阅读3.1w次,点赞19次,收藏48次。前两天在学习linux虚拟化的时候, 发现虚拟机磁盘空间不足, 由于当初分区的时候不是用lvm来分区的, 导致无法扩容, 所以只能新建了一台虚拟机来学习. 然而在新建完成后, 按照之前老师教的一系列优化手段, 将这台手段优化到我认为最优的条件下, 重启了一下系统, 发现登录界面自动回到了字符界面. 这可把我给郁闷坏了, 好端端的怎么就变成了字符界面了呢? 于是输..._centos startx

RoseHA集群:RHEL+RoseMirror+Oracle【2】-程序员宅基地

文章浏览阅读391次。博主正在参加“2011年度IT博客大赛”,欢迎帮顶! @投票地址@---------------------------------------------三.安装Rose Mirror HA:·准备工作: 在安装 Rose Mirror HA 前检查相应的软件包是否安装,如果没有安装通过系统安装盘进行安装。要正常的运行 ..._rose ha监控脚本

nox夜神模拟器连接不上android studio,用bat脚本快速输入命令_电脑使用夜神模拟器教程,bat文件-程序员宅基地

文章浏览阅读230次。用bat命令快速使android studio连接夜神模拟器_电脑使用夜神模拟器教程,bat文件