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

WBOY
Release: 2016-06-13 12:07:09
Original
1474 people have browsed it

求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 流式文件   世界清静了不少

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!