求phpexcel處理大檔案時記憶體佔用過大的解決方案,該如何處理

來源:互聯網
上載者:User
求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 <= $all_row; $row_index++)
{
$row_val = '';
for ($col_index = 'A'; $col_index <= $all_column; $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 流式檔案 世界清靜了不少
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.