PHPExcel-如何逐行读取Excel工作表

Noufalcep

如何使用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>';
马克·贝克

如果您只想读取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);

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

PHPExcel:逐行读取 Excel 文件

来自分类Dev

PHPExcel-如何使用工作表ID读取Excel工作表

来自分类Dev

PHPExcel-如何使用工作表ID读取Excel工作表

来自分类Dev

如何使用phpexcel在Excel工作表中插入公式

来自分类Dev

如何使用phpexcel在Excel工作表中插入公式

来自分类Dev

PhpExcel创建多个工作表

来自分类Dev

创建新的工作表PHPExcel

来自分类Dev

PHPExcel从CSV读取日期

来自分类Dev

如何限制PHPExcel读取的列?

来自分类Dev

使用phpexcel从其他工作表中读取公式

来自分类Dev

PHPexcel获得最高的工作表索引范围

来自分类Dev

PHPExcel-查找工作表状态

来自分类Dev

在phpexcel中复制工作表时出错

来自分类Dev

PHPExcel从大型工作表中删除行

来自分类Dev

PHPExcel - 如果工作表名称等于,则继续

来自分类Dev

如何使用PHPExcel将多个HTML表单数据添加到Excel工作表中

来自分类Dev

如何使用PHPExcel读取受密码保护的Excel文件?

来自分类Dev

Symfony 2:如何在PHPExcel中读取Excel文件内容

来自分类Dev

phpexcel日期读取的格式错误

来自分类Dev

用PHPExcel读取命名表

来自分类Dev

用PHPExcel读取命名表

来自分类Dev

PHPExcel下载无法正常工作

来自分类Dev

PHPExcel下载无法正常工作

来自分类Dev

phpexcel不创建新表

来自分类Dev

使用PHPExcel保护Excel文件

来自分类Dev

基本的PHPExcel输出Excel文件

来自分类Dev

PHPExcel-如何设置网址

来自分类Dev

如何为pdf配置phpexcel

来自分类Dev

PHPExcel-如何设置网址