php读取excel 报错_php读取excel内存溢出

在使用PHP读取大量数据的Excel文件时,经常遇到内存不足的问题。通过使用PHPExcel的过滤器进行块级读取可以缓解此问题,但即使如此,内存仍可能无法正确释放。解决方案是在读取完每个块后调用PHPExcel的disconnectWorksheets()方法,这将释放对象引用,从而释放内存。确保在读取过程中及时清理资源以防止内存耗尽。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

php使用phpexcel读取excel的时候,如果excel本身行列较多,很容易引起

Allowed memory size of 134217728 bytes exhausted (tried to allocate 4096 bytes)

内存不够的报错,

于是我写了一段脚本,利用phpexcel过滤器实现块级读取,但是每次读取完毕了,占用的内存不会释放掉,造成多次读取以后还是会报出Allowed memory size of 134217728 bytes exhausted (tried to allocate 4096 bytes)内存不够的错误?

使用unset,=null都没有明显的作用,后面查了下资料,

在PHPExcel_Worksheet和phpexcel类中加了Desroty方法,并且显式调用也没有用

public function Destroy() {

foreach($this->_cellCollection as $index => $dummy) {

$this->_cellCollection[$index] = null;

}

$this->_cellCollection = null;

}

public function Destroy() {

foreach($this->_workSheetCollection as $index => $dummy) {

$this->_workSheetCollection[$index]->Destroy();

$this->_workSheetCollection[$index] = null;

}

$this->_workSheetCollection = null;

}

已找到解决问题答案,每次load完excel文件并且读取完成以后,用load产生的phpexcel对象调用一下phpexcel本身提供的disconnectWorksheets()方法,会为phpexcel的属性赋予null值,释放内存资源

这种方法听说可以逐行导入内存 ,待测试https://phpspreadsheet.readthedocs.io/en/latest/topics/reading-files/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值