1

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 
    } 

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.