热搜:NVER node 开发 php

求phpexcel处理大文件时内存占用过大的解决方案

2024-08-21 11:25:02
求phpexcel处理大文件时内存占用过大的解决方案

phpexcel由于是内存处理机制,当excel文件行数或列数过多时占用内存会瞬间几百M,由于服务器原因我无法申请更多的内存,所以只能想法从根本上解决或规避问题,比方说处理完一个excel文件或其中一个sheet页就释放内存然后再处理下一个,网上搜了一圈,没一个写的详细完整的,求大神帮忙调通,非常期待。。

PS:
1. 我使用的版本是1.76,设置过使用文件之类的参数,完全不起作用;
2. 小文件已调通
3. 上面已经提到了,不用再说调大内存限制了




处理excel代码如下:执行报错
Fatal error: Using $this when not in object context in E:\wamp\www\uploadify\PHPExcel.php on line 796

function parse_excel_file($filename) 
{
$php_reader = new PHPExcel_Reader_Excel2007();
$php_reader = PHPExcel_IOFactory::createReaderForFile($filename); 
$php_excel = $php_reader->load($filename);
$current_sheet = $php_excel->getSheet(0);
$all_column = $current_sheet->getHighestColumn();
$all_row = $current_sheet->getHighestRow();
$list = array();
for ($row_index = 2; $row_index  {
$row_val = '';
for ($col_index = 'A'; $col_index  {
$addr = $col_index . $row_index;
$cell = $current_sheet->getCell($addr)->getValue();
if ($cell instanceof PHPExcel_RichText) 
{
$cell = $cell->__toString();

$row_val = $row_val . ',' . $cell;
}
array_push($list, $row_val);
}
PHPExcel::Destroy();
return $list;
}

phpexcel.php中增加代码如下:
public function Destroy() {
foreach($this->_workSheetCollection as $index => $dummy) {
$this->_workSheetCollection[$index]->Destroy();
$this->_workSheetCollection[$index] = null;
}
}

worksheet.php中增加代码如下:
public function Destroy() {
foreach($this->_cellCollection as $index => $dummy) {
$this->_cellCollection[$index] = null;
}
}


回复讨论(解决方案)

一次性导出多少条?

调用方式应该是:$php_excel ->Destroy();

phpexcel 库耗内存太严重  计算时全部在内存缓存 
曾经ini_set('memory_limit','1024M');只能导出1W条数据   
最后我们采用了csv 流式文件   世界清静了不少

调用方式应该是:$php_excel ->Destroy();

phpexcel 库耗内存太严重  计算时全部在内存缓存 
曾经ini_set('memory_limit','1024M');只能导出1W条数据   
最后我们采用了csv 流式文件   世界清静了不少



感谢提示,用了你的写法确实不报错了,但是内存没有destroy成功,还是在一次次增长,你说的csv是不用插件直接导出那种还是phpexcel的导出csv?我这里是读取