代码点火器用PHP Excel读取excel文件

时间:2013-01-10 19:08:15

标签: php codeigniter phpexcel

我想从具有未指定行数和指定列数的Excel工作表中读取。我只能用指定的行数来做,任何解决方案都会有很大的帮助

    $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 个答案:

答案 0 :(得分:1)

使用以下方法处理后,您应该真正卸载每个块:

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

如开发人员文档的第4.3节所述,否则在每个块之后可能无法清除内存。