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

WBOY
發布: 2016-06-13 12:07:09
原創
1474 人瀏覽過

求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.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!