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

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
Golang 网络安全与加密实战
Golang 网络安全与加密实战

本专题系统讲解 Golang 在网络安全与加密技术中的应用,包括对称加密与非对称加密(AES、RSA)、哈希与数字签名、JWT身份认证、SSL/TLS 安全通信、常见网络攻击防范(如SQL注入、XSS、CSRF)及其防护措施。通过实战案例,帮助学习者掌握 如何使用 Go 语言保障网络通信的安全性,保护用户数据与隐私。

2

2026.01.29

俄罗斯Yandex引擎入口
俄罗斯Yandex引擎入口

2026年俄罗斯Yandex搜索引擎最新入口汇总,涵盖免登录、多语言支持、无广告视频播放及本地化服务等核心功能。阅读专题下面的文章了解更多详细内容。

481

2026.01.28

包子漫画在线官方入口大全
包子漫画在线官方入口大全

本合集汇总了包子漫画2026最新官方在线观看入口,涵盖备用域名、正版无广告链接及多端适配地址,助你畅享12700+高清漫画资源。阅读专题下面的文章了解更多详细内容。

156

2026.01.28

ao3中文版官网地址大全
ao3中文版官网地址大全

AO3最新中文版官网入口合集,汇总2026年主站及国内优化镜像链接,支持简体中文界面、无广告阅读与多设备同步。阅读专题下面的文章了解更多详细内容。

296

2026.01.28

php怎么写接口教程
php怎么写接口教程

本合集涵盖PHP接口开发基础、RESTful API设计、数据交互与安全处理等实用教程,助你快速掌握PHP接口编写技巧。阅读专题下面的文章了解更多详细内容。

10

2026.01.28

php中文乱码如何解决
php中文乱码如何解决

本文整理了php中文乱码如何解决及解决方法,阅读节专题下面的文章了解更多详细内容。

13

2026.01.28

Java 消息队列与异步架构实战
Java 消息队列与异步架构实战

本专题系统讲解 Java 在消息队列与异步系统架构中的核心应用,涵盖消息队列基本原理、Kafka 与 RabbitMQ 的使用场景对比、生产者与消费者模型、消息可靠性与顺序性保障、重复消费与幂等处理,以及在高并发系统中的异步解耦设计。通过实战案例,帮助学习者掌握 使用 Java 构建高吞吐、高可靠异步消息系统的完整思路。

10

2026.01.28

Python 自然语言处理(NLP)基础与实战
Python 自然语言处理(NLP)基础与实战

本专题系统讲解 Python 在自然语言处理(NLP)领域的基础方法与实战应用,涵盖文本预处理(分词、去停用词)、词性标注、命名实体识别、关键词提取、情感分析,以及常用 NLP 库(NLTK、spaCy)的核心用法。通过真实文本案例,帮助学习者掌握 使用 Python 进行文本分析与语言数据处理的完整流程,适用于内容分析、舆情监测与智能文本应用场景。

25

2026.01.27

拼多多赚钱的5种方法 拼多多赚钱的5种方法
拼多多赚钱的5种方法 拼多多赚钱的5种方法

在拼多多上赚钱主要可以通过无货源模式一件代发、精细化运营特色店铺、参与官方高流量活动、利用拼团机制社交裂变,以及成为多多进宝推广员这5种方法实现。核心策略在于通过低成本、高效率的供应链管理与营销,利用平台社交电商红利实现盈利。

125

2026.01.26

热门下载

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

精品课程

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

共137课时 | 10.1万人学习

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

共6课时 | 11.2万人学习

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

共13课时 | 0.9万人学习

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

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