PHPExcel - How can i read the Excel sheet row by row

前端 未结 4 1686
长发绾君心
长发绾君心 2020-12-11 16:41

How can i read Excel worksheet row by row using PHPExcel? I have a sheet contains more than 100000 rows, but i want to read only 500 rows.

$sheetData = $objP         


        
相关标签:
4条回答
  • 2020-12-11 17:05

    You can load 500 rows or even setting start index by setting parameters directly to the getRowIterator function.

    $path = "Your File Path HERE";
    $fileObj = \PHPExcel_IOFactory::load( $path );
    $sheetObj = $fileObj->getActiveSheet();
    $startFrom = 50; //default value is 1
    $limit = 550; //default value is null
    foreach( $sheetObj->getRowIterator($startFrom, $limit) as $row ){
        foreach( $row->getCellIterator() as $cell ){
            $value = $cell->getCalculatedValue();
        }
    }
    
    0 讨论(0)
  • 2020-12-11 17:11

    If you only want to read 500 rows, then only load 500 rows using a read filter:

    $inputFileType = 'Excel5';
    $inputFileName = './sampleData/example2.xls';
    
    
    /**  Define a Read Filter class implementing PHPExcel_Reader_IReadFilter  */
    class myReadFilter 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 rows that are configured in $this->_startRow and $this->_endRow
            if (($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 = 500;
    /**  Create a new Instance of our Read Filter  **/
    $chunkFilter = new myReadFilter();
    
    /**  Tell the Reader that we want to use the Read Filter that we've Instantiated  **/
    $objReader->setReadFilter($chunkFilter);
    
    /**  Tell the Read Filter, the limits on which rows we want to read this iteration  **/
    $chunkFilter->setRows(1,500);
    /**  Load only the rows that match our filter from $inputFileName to a PHPExcel Object  **/
    $objPHPExcel = $objReader->load($inputFileName);
    

    See the PHPExcel User Documentation - Reading Spreadsheet Files document in /Documentation for more details on Read Filters

    0 讨论(0)
  • 2020-12-11 17:23

    This loops through all rows and columns and assigns the cell value to $cellValue. If the $i is greater than 500, it breaks out the loop

    $objWorksheet = $objPHPExcel->getActiveSheet();
    
    
    $i = 0;
    foreach ($objWorksheet->getRowIterator() as $row) {
        if ($i > 500) break;
        $i++;
    
        foreach ($row->getCellIterator() as $cell) {
            $cellValue = trim($cell->getCalculatedValue());
        }
    }
    
    0 讨论(0)
  • 2020-12-11 17:26
    require_once "/PATH TO PHP EXCEL FOLDER/PHPExcel.php"; 
    $inputFileName = $_FILES['FILENAME'];
    $objTpl = PHPExcel_IOFactory::load('./PATH TO UPLOAD FOLDER/' . $inputFileName);
    $sheetData = $objTpl->getActiveSheet()->toArray(null, true, true, true);
    array_shift($sheetData);
    $i=0;
    $test_array = array();
    foreach($sheetData as $key=>$val){
        if($i < 500)
            $test_array[$i] = $val;
        $i++;   
    }
    
    0 讨论(0)
提交回复
热议问题