PHPExcel大檔案塊層級讀取 速度快 減少佔用資源

來源:互聯網
上載者:User

標籤: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大檔案塊層級讀取 速度快 減少佔用資源

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.