標籤:log div ring clu factor font ade 讀取 讀取內容
/** * 讀取excel轉換成數組 * * @param string $excelFile 檔案路徑 * @param int $startRow 開始讀取的行數 * @param int $endRow 結束讀取的行數 * @return array */ private function readFromExcel($excelFile, $startRow = 1, $endRow = 100) { include_once ‘./Core/Common/PHPExcel.php‘; include_once ‘./Core/Common/PHPExcelReadFilter.php‘; $excelType = PHPExcel_IOFactory::identify($excelFile); $excelReader = \PHPExcel_IOFactory::createReader($excelType); if(strtoupper($excelType) == ‘CSV‘) { $excelReader->setInputEncoding(‘GBK‘); } if ($startRow && $endRow) { $excelFilter = new PHPExcelReadFilter(); $excelFilter->startRow = $startRow; $excelFilter->endRow = $endRow; $excelReader->setReadFilter($excelFilter); } $phpexcel = $excelReader->load($excelFile); $activeSheet = $phpexcel->getActiveSheet(); $highestColumn = $activeSheet->getHighestColumn(); //最後列數所對應的字母,例如第1行就是A $highestColumnIndex = \PHPExcel_Cell::columnIndexFromString($highestColumn); //總列數 $data = array(); for ($row = $startRow; $row <= $endRow; $row++) { for ($col = 0; $col < $highestColumnIndex; $col++) { $data[$row][] = (string) $activeSheet->getCellByColumnAndRow($col, $row)->getValue(); } if(implode($data[$row], ‘‘) == ‘‘) { unset($data[$row]); } } return $data; }
$rowSize = 200; $startRow = 2;//從第二行開始讀取$endRow = $rowSize;$excel_orders = array();while (true) { $excel_orders = $this->readFromExcel(dirname(dirname(HOME_PATH)).‘/Upload/‘.$newname, $startRow, $endRow); if(empty($excel_orders)) { break; } $startRow = $endRow + 1; $endRow = $endRow + $rowSize;}
/** * 讀取excel過濾器類 單獨檔案 */class PHPExcelReadFilter implements PHPExcel_Reader_IReadFilter { public $startRow = 1; public $endRow; public function readCell($column, $row, $worksheetName = ‘‘) { if (!$this->endRow) { return true; } if ($row >= $this->startRow && $row <= $this->endRow) { return true; } return false; }}
描述:使用者匯入訂單資料,但使用者匯入8k左右的資料之後,PHP開始爆紅了。
查閱部分資料得知,是由於PHPExcel的類讀取,如果使用
$PHPExcel = $PHPReader->load($newfilename);
load的方式讀取檔案的話,是把檔案中的全部內容讀出並儲存在記憶體中,再讀取內容的話,就是直接從記憶體中讀取,極其消耗資源。
而以上程式碼片段實現的效果就是我需要那塊(n-m行)的內容,我唯讀去那部分的內容,不會載入整個檔案。
最後,使用完變數,可以進行 $a = null; / unset($a); 進行釋放資源。
祝工作順利!
PHPExcel大檔案塊層級讀取 速度快 減少佔用資源