致命错误:使用phpexcel写入xlsx文件时发生内存不足(分配1979711488)(试图分配131072字节)错误 [英] Fatal error: Out of memory (allocated 1979711488) (tried to allocate 131072 bytes) error occur while writing xlsx file using phpexcel

查看:196
本文介绍了致命错误:使用phpexcel写入xlsx文件时发生内存不足(分配1979711488)(试图分配131072字节)错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经集成了xlsx文件,可以使用phpexcel从数据库写入数据.我想在xlsx文件中写入300万条记录.但直到出现致命错误:内存不足(已分配1979711488)(试图分配131072字节)

I have integrating xlsx file for writing from database using phpexcel. I want to write 3,00,000 records in xlsx file. But it till through Fatal error: Out of memory (allocated 1979711488) (tried to allocate 131072 bytes)

我的PHP版本5.3.28

My PHP Version 5.3.28

我还设置了php ini和单元格缓存,请参见下面的代码

Also i set php ini and cell cache see my code below

ini_set('max_execution_time',-1);
ini_set('memory_limit', '-1'); 
$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_in_memory_gzip;
$cacheSettings = array( ' memoryCacheSize ' => '-1');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);

期待您的答复.

谢谢

推荐答案

由于您尝试编写大量记录,因此即使使用PHPExcel提供的缓存技术,您仍然可能会遇到内存不足错误.否则您的脚本将花费很长时间才能完成(太长以至于无法接受).

Because you are trying to write a very large amount of records, it may be possible that even if you use the caching techniques PHPExcel provides, you'd still encounter OutOfMemory errors. Or that your script would take too long to finish (too long to be acceptable).

如果您看到这种情况,我建议您看一下 Spout : https://github.com/box/spout .它是为完全解决您的问题而创建的PHPExcel的替代方法.您可以根据需要抛出任意数量的记录,该库将能够编写它们而无需任何额外的配置.

If this is something you see happening, I can recommend you taking a look at Spout: https://github.com/box/spout. It's an alternative to PHPExcel that was created to solve exactly your problem. You can throw it as many records as you want, the library will be able to write them without needing any extra config.

编写XLSX文件非常简单:

Writing a XLSX file is that easy:

$writer = WriterFactory::create(Type::XLSX);
$writer->openToFile($filePath);

while (...) {
    $writer->addRow($singleRow);
}

$writer->close();

这篇关于致命错误:使用phpexcel写入xlsx文件时发生内存不足(分配1979711488)(试图分配131072字节)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆