0

0

PHPExcel导出大量数据超时及内存错误解决方法

巴扎黑

巴扎黑

发布时间:2016-11-11 14:50:36

|

2473人浏览过

|

来源于php中文网

原创

php导出excel相对很多童鞋都碰到了,使用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啥时候能出一个轻量级的版本,不需要那么多花哨的功能,只需要导出最普通的数据的版本就好了! 

以上内容是lz在网上找的一篇比较不错的文章,虽没有彻底解决我的问题,但感觉写的不错,谨以此作为笔记,希望对看到的童鞋能够有所帮助!

相关文章

WPS零基础入门到精通全套教程!
WPS零基础入门到精通全套教程!

全网最新最细最实用WPS零基础入门到精通全套教程!带你真正掌握WPS办公! 内含Excel基础操作、函数设计、数据透视表等

下载

相关标签:

php

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Java 项目构建与依赖管理(Maven / Gradle)
Java 项目构建与依赖管理(Maven / Gradle)

本专题系统讲解 Java 项目构建与依赖管理的完整体系,重点覆盖 Maven 与 Gradle 的核心概念、项目生命周期、依赖冲突解决、多模块项目管理、构建加速与版本发布规范。通过真实项目结构示例,帮助学习者掌握 从零搭建、维护到发布 Java 工程的标准化流程,提升在实际团队开发中的工程能力与协作效率。

8

2026.01.12

c++主流开发框架汇总
c++主流开发框架汇总

本专题整合了c++开发框架推荐,阅读专题下面的文章了解更多详细内容。

101

2026.01.09

c++框架学习教程汇总
c++框架学习教程汇总

本专题整合了c++框架学习教程汇总,阅读专题下面的文章了解更多详细内容。

55

2026.01.09

学python好用的网站推荐
学python好用的网站推荐

本专题整合了python学习教程汇总,阅读专题下面的文章了解更多详细内容。

139

2026.01.09

学python网站汇总
学python网站汇总

本专题整合了学python网站汇总,阅读专题下面的文章了解更多详细内容。

12

2026.01.09

python学习网站
python学习网站

本专题整合了python学习相关推荐汇总,阅读专题下面的文章了解更多详细内容。

19

2026.01.09

俄罗斯手机浏览器地址汇总
俄罗斯手机浏览器地址汇总

汇总俄罗斯Yandex手机浏览器官方网址入口,涵盖国际版与俄语版,适配移动端访问,一键直达搜索、地图、新闻等核心服务。

86

2026.01.09

漫蛙稳定版地址大全
漫蛙稳定版地址大全

漫蛙稳定版地址大全汇总最新可用入口,包含漫蛙manwa漫画防走失官网链接,确保用户随时畅读海量正版漫画资源,建议收藏备用,避免因域名变动无法访问。

447

2026.01.09

php学习网站大全
php学习网站大全

精选多个优质PHP入门学习网站,涵盖教程、实战与文档,适合零基础到进阶开发者,助你高效掌握PHP编程。

49

2026.01.09

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP课程
PHP课程

共137课时 | 8.5万人学习

JavaScript ES5基础线上课程教学
JavaScript ES5基础线上课程教学

共6课时 | 6.9万人学习

PHP新手语法线上课程教学
PHP新手语法线上课程教学

共13课时 | 0.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号