1

我想从具有未指定行数和指定列数的 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 
    } 
4

1 回答 1

1

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

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

如开发者文档第 4.3 节所述,否则可能不会在每个块之后清除内存。

于 2013-01-10T21:04:39.747 回答