简体   繁体   中英

Code igniter reading an excel file with PHP Excel

I would like to read from an excel sheet with unspecified number of rows and a specified number of columns. I'm only able to do it with specified number of rows, Any solutions will be of great help

    $inputFileType = 'Excel5'; 
    $inputFileName = './sampleData/example2.xls'; 


    /**  Define a Read Filter class implementing PHPExcel_Reader_IReadFilter  */ 
    class chunkReadFilter implements PHPExcel_Reader_IReadFilter 
    { 
        private $_startRow = 0; 
        private $_endRow   = 0; 

        /**  Set the list of rows that we want to read  */ 
        public function setRows($startRow, $chunkSize) { 
            $this->_startRow = $startRow; 
            $this->_endRow   = $startRow + $chunkSize; 
        } 

        public function readCell($column, $row, $worksheetName = '') { 
            //  Only read the heading row, and the configured rows 
            if (($row == 1) ||
                ($row >= $this->_startRow && $row < $this->_endRow)) { 
                return true; 
            } 
            return false; 
        } 
    } 


    /**  Create a new Reader of the type defined in $inputFileType  **/ 
    $objReader = PHPExcel_IOFactory::createReader($inputFileType); 


    /**  Define how many rows we want to read for each "chunk"  **/ 
    $chunkSize = 2048; 
    /**  Create a new Instance of our Read Filter  **/ 
    $chunkFilter = new chunkReadFilter(); 

    /**  Tell the Reader that we want to use the Read Filter  **/ 
    $objReader->setReadFilter($chunkFilter); 
      //I want to loop and get values from the excel sheet startrow< undefined number of rows 
    for ($startRow = 2; $startRow <= 65536; $startRow += $chunkSize) {

        /**  Tell the Read Filter which rows we want this iteration  **/ 
        $chunkFilter->setRows($startRow,$chunkSize); 
        /**  Load only the rows that match our filter  **/ 
        $objPHPExcel = $objReader->load($inputFileName); 
        //    Do some processing here 
    } 

You should really unload each chunk after processing it using:

$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);

as described in section 4.3 of the developer documentation, otherwise memory may not be cleared after each chunk.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM