PHPExcel taking an extremely long time to read Excel file

后端 未结 4 669
礼貌的吻别
礼貌的吻别 2021-01-14 08:11

I\'m using PHPExcel 1.7.8, PHP 5.4.14, Windows 7, and an Excel 2007 spreadsheet. The spreadsheet consists of 750 rows, columns A through BW, and is about 600KB in size. This

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-14 08:15

    PHPExcel has a problem with identifying where the end of your excel file is. Or rather, Excel has a hard time knowing where the end of itself is. If you touch a cell at A:1000000 it thinks it needs to read that far.

    I have done 2 things in the past to fix this:

    1) Cut and past the data you need into new excel file. 2) Specify the exact dimensions you want to read.

    Edit How to do option 2

    public function readExcelDataToArray($excelFilePath, $maxRowNumber=-1, $maxColumnNumber=-1)
    {
        $objPHPExcel = PHPExcel_IOFactory::load($excelFilePath);
        $objWorksheet = $objPHPExcel->getActiveSheet();
    
        //Get last row and column that have data
        if ($maxRowNumber == -1){
        $lastRow = $objWorksheet->getHighestDataRow();
        } else {
            $lastRow = $maxRowNumber;
        }
    
        if ($maxColumnNumber == -1){
            $lastCol = $objWorksheet->getHighestDataColumn();
            //Change Column letter to column number
            $lastCol = PHPExcel_Cell::columnIndexFromString($lastCol);      
        } else {
            $lastCol = $maxColumnNumber;
        }   
    
        //Get Data Array
        $dataArray = array();
    
        for ($currentRow = 1; $currentRow <= $lastRow; $currentRow++){
            for ($currentCol = 0; $currentCol <= $lastCol; $currentCol++){
                $dataArray[$currentRow][$currentCol] = $objWorksheet->getCellByColumnAndRow($currentCol,, $currentRow)->getValue();
            }
        }
        return $dataArray;
    }
    

    Unfortunately these solutions aren't very dynamic.

    Note that a modern excel file is really just a zip with an xlsx extension. I have written extensions to PHPExcel that unzip them, and modify certain xml files to get the kinds of behaviors I want.

    A third suggestion for you would be to monitor the contents of each row and stop when you get an empty one.

提交回复
热议问题