【问题标题】:PHPExcel - How can i read the Excel sheet row by rowPHPExcel - 我如何逐行读取 Excel 工作表
【发布时间】:2014-07-31 10:00:23
【问题描述】:

如何使用 PHPExcel 逐行读取 Excel 工作表?我有一张包含超过 100000 行的工作表,但我只想读取 500 行。

$sheetData = $objPHPExcel->getActiveSheet();
$highestRow = $sheetData->getHighestRow(); 
$highestColumn = $sheetData->getHighestColumn(); 
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); 

echo '<table>';
for ($row = 1; $row <= $highestRow; ++$row) {
    echo '<tr>';

    for ($col = 0; $col <= $highestColumnIndex; ++$col) {
    echo '<td>' . $sheetData->getCellByColumnAndRow($col, $row)->getValue() . '</td>';
    }

    echo '</tr>';
}
echo '</table>';

【问题讨论】:

    标签: php phpexcel


    【解决方案1】:

    如果您只想读取 500 行,那么使用读取过滤器只加载 500 行:

    $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);
    

    有关读取过滤器的更多详细信息,请参阅 /Documentation 中的 PHPExcel User Documentation - Reading Spreadsheet Files 文档

    【讨论】:

    【解决方案2】:

    您可以通过直接向 getRowIterator 函数设置参数来加载 500 行甚至设置起始索引。

    $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();
        }
    }
    

    【讨论】:

    • 注意,仅适用于 PHPExcel >= 1.8.1
    【解决方案3】:

    这将遍历所有行和列并将单元格值分配给 $cellValue。如果 $i 大于 500,则打破循环

    $objWorksheet = $objPHPExcel->getActiveSheet();
    
    
    $i = 0;
    foreach ($objWorksheet->getRowIterator() as $row) {
        if ($i > 500) break;
        $i++;
    
        foreach ($row->getCellIterator() as $cell) {
            $cellValue = trim($cell->getCalculatedValue());
        }
    }
    

    【讨论】:

    • 我已经试过了。但在这种情况下,变量 $objWorksheet 存储整个 excel 数据。在大数据的情况下,它不起作用。
    • 什么不起作用,阅读?或者加载电子表格开始?发布一些您已经输入的代码。
    • 我已经阅读了大约 5000 行的 excelsheet。但是在处理高达 10 万行的较大文件时,页面不会加载。
    • 我建议你看看马克贝克的回答,然后使用读取过滤器来减轻负担。
    【解决方案4】:
    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++;   
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-06
      • 2011-06-07
      • 1970-01-01
      • 2022-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多