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 流式檔案 世界清靜了不少
調用方式應該是:$php_excel ->Destroy();
phpexcel 庫耗記憶體太嚴重 計算時全部在記憶體緩衝
曾經ini_set('memory_limit','1024M');只能匯出1W條資料
最後我們採用了csv 流式檔案 世界清靜了不少
感謝提示,用了你的寫法確實不報錯了,但是記憶體沒有destroy成功,還是在一次次增長,你說的csv是不用外掛程式直接匯出那種還是phpexcel的匯出csv?我這裡是讀取