设置20个单元格类型后,PhpExcel停止工作

我有一个生成一个小xls表(~25×15)的脚本.它包含百分比和字符串,我有一个if运算符,使用以下代码将当前单元格设置为百分比类型:
$this->objPHPExcel->getActiveSheet()->getStyle($coords)->getNumberFormat()->setFormatCode('0.00%');

但是当我导出并查看文件时,我发现它只设置了约20个单元格的类型和样式.其余所有都是认设置.我调试它并意识到问题不在我的逻辑中.我读到了增加PHP缓存内存 – 尝试了但它没有用.请帮忙,因为我需要出口至少15倍的桌子.提前致谢!

PHPExcel分配了相当多的内存

虽然PHPExcel是一个漂亮的库,但使用它可能需要为PHP分配大量内存.

根据this thread,只有5个单元可以渲染6 MB的内存使用量:

<?PHP
require_once 'PHP/PHPExcelSVN/PHPExcel/IOFactory.PHP';
$objPHPExcel = PHPExcel_IOFactory::load("PHP/tinytest.xlsx");
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('D2',50);
echo $objPHPExcel->getActiveSheet()->getCell('D8')->getCalculatedValue() . "
";
echo date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB\r\n";
?>

I get 6MB of memory usage.

一个user even failed with a 256MByte memory设置.

虽然PHPExcel提供了减少内存占用的方法,但在我的情况下,所有减少都变得太小了. This page on github提供了PHPExcel缓存管理选项的详细信息.例如,此设置序列化和GZIPs工作表的单元格结构:

$cacheMethod = PHPExcel_CachedobjectStorageFactory::cache_in_memory_gzip;

PHPExcel_Settings::setCacheStorageMethod($cacheMethod);

PHPExcel’s FAQ解释了这个:

Fatal error: Allowed memory size of xxx bytes exhausted (tried to
allocate yyy bytes) in zzz on line aaa

PHPExcel holds an “in memory” representation of a spreadsheet,so it
is susceptible to PHP’s memory limitations. The memory made available
to PHP can be increased by editing the value of the memorylimit
directive in your PHP.ini file,or by using iniset(‘memory_limit’,
‘128M’) within your code (ISP permitting);

Some Readers and Writers are faster than others,and they also use
differing amounts of memory. You can find some indication of the
relative performance and memory usage for the different Readers and
Writers,over the different versions of PHPExcel,here
07004

If you’ve already increased memory to a maximum,or can’t change your
memory limit,then this discussion on the board describes some of the
methods that can be applied to reduce the memory usage of your scripts
using PHPExcel
07005

PHP Excel的测量结果

我检测了PHPExcel示例文件[01simple.PHP] [5]并做了一些快速测试.

消耗92 KByte:

for( $n=0; $n<200; $n++ ) {
    $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A' . $n,'Miscellaneous glyphs');
}

消耗4164 KB:

for( $n=0; $n<200; $n++ ) {
    $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A' . $n,'Miscellaneous glyphs');
    $objPHPExcel->getActiveSheet()->getStyle('A' . $n)->getAlignment()->setWrapText(true);
}

如果多次执行此片段不变,则每个片段消耗大约4 MB.

检查您的应用在逻辑上是正确的

为确保您的应用程序在逻辑上正确,我建议首先增加PHP内存:

ini_set('memory_limit','32M');

在我的情况下,我必须导出到在线评估应用程序的导出结果数据.虽然水平方向的单元格少于100个,但我需要导出多达10,000个行.虽然单元格的数量很大,但我的每个单元格都包含3个字符的数字或字符串 – 没有公式,没有样式.

如果存在强大的内存限制或大型电子表格

在我的例子中,没有一个缓存选项减少了所需的数量.此外,应用程序的运行时间也大大增加.

最后我不得不切换到老式的CSV数据文件导出.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...