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在网上找的一篇比较不错的文章,虽没有彻底解决我的问题,但感觉写的不错,谨以此作为笔记,希望对看到的童鞋能够有所帮助!
0
0
相关文章
PHP字符串转数组怎么做_PHP字符串分割函数使用【操作】
Hyperf跨版本兼容怎么测试_Hyperf升级测试方法【解答】
PHP JSON编码怎么实现_PHP json_encode使用【技巧】
Symfony控制台内存溢出_增加PHP内存限制【操作】
Swoole故障排查方法是什么_Swoole错误诊断步骤【解答】
相关标签:
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
本专题围绕 C# 在现代后端架构中的微服务实践展开,系统讲解基于 ASP.NET Core 构建可扩展服务体系的核心方法。内容涵盖服务拆分策略、RESTful API 设计、服务间通信、API 网关统一入口管理以及服务治理机制。通过真实项目案例,帮助开发者掌握构建高可用微服务系统的关键技术,提高系统的可扩展性与维护效率。
76
2026.03.11
本专题围绕 Go 语言在高并发任务处理场景中的实践展开,系统讲解 Goroutine 调度模型、Channel 通信机制以及并发控制策略。内容包括任务队列设计、Goroutine 池化管理、资源限制控制以及并发任务的性能优化方法。通过实际案例演示,帮助开发者构建稳定高效的 Go 并发任务处理系统,提高系统在高负载环境下的处理能力与稳定性。
38
2026.03.10
本专题围绕 Kotlin 在 Android 应用开发中的架构实践展开,重点讲解模块化设计与组件化开发的实现思路。内容包括项目模块拆分策略、公共组件封装、依赖管理优化、路由通信机制以及大型项目的工程化管理方法。通过真实项目案例分析,帮助开发者构建结构清晰、易扩展且维护成本低的 Android 应用架构体系,提升团队协作效率与项目迭代速度。
83
2026.03.09
本专题围绕 JavaScript 在浏览器中的执行与渲染机制展开,系统讲解 DOM 构建、CSSOM 解析、重排与重绘原理,以及关键渲染路径优化方法。内容涵盖事件循环机制、异步任务调度、资源加载优化、代码拆分与懒加载等性能优化策略。通过真实前端项目案例,帮助开发者理解浏览器底层工作原理,并掌握提升网页加载速度与交互体验的实用技巧。
97
2026.03.06
本专题围绕 Rust 语言核心特性展开,深入讲解所有权机制、借用规则、生命周期管理以及智能指针等关键概念。通过系统级开发案例,分析内存安全保障原理与零成本抽象优势,并结合并发场景讲解 Send 与 Sync 特性实现机制。帮助开发者真正理解 Rust 的设计哲学,掌握在高性能与安全性并重场景中的工程实践能力。
223
2026.03.05
本专题围绕 PHP 在现代 Web 后端开发中的高性能实践展开,重点讲解基于 Laravel 框架构建可扩展 API 服务的核心方法。内容涵盖路由与中间件机制、服务容器与依赖注入、接口版本管理、缓存策略设计以及队列异步处理方案。同时结合高并发场景,深入分析性能瓶颈定位与优化思路,帮助开发者构建稳定、高效、易维护的 PHP 后端服务体系。
458
2026.03.04
2026最全AI工具安装教程专题:包含各版本AI绘图、AI视频、智能办公软件的本地化部署手册。全篇零基础友好,附带最新模型下载地址、一键安装脚本及常见报错修复方案。每日更新,收藏这一篇就够了,让AI安装不再报错!
169
2026.03.04
本专题聚焦 Swift 在 iOS 应用架构设计中的实践,系统讲解 MVVM 模式的核心思想、数据绑定机制、模块拆分策略以及组件化开发方法。内容涵盖网络层封装、状态管理、依赖注入与性能优化技巧。通过完整项目案例,帮助开发者构建结构清晰、可维护性强的 iOS 应用架构体系。
246
2026.03.03
本专题围绕 C++ 在高性能网络服务开发中的应用展开,深入讲解 Socket 编程、多路复用机制、Reactor 模型设计原理以及线程池协作策略。内容涵盖 epoll 实现机制、内存管理优化、连接管理策略与高并发场景下的性能调优方法。通过构建高并发网络服务器实战案例,帮助开发者掌握 C++ 在底层系统与网络通信领域的核心技术。
34
2026.03.03
热门下载
相关下载
精品课程
最新文章

