php 只需从 PHPExcel 获取一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17951067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:42:52  来源:igfitidea点击:

Just get one row from PHPExcel

phpphpexcel

提问by Bart Friederichs

I tried to find a way to just get a row using PHPExcel. After numerous searches on the internet, I only found a way to iterate over them, with an optional start row. This lead to the following solution:

我试图找到一种方法来使用 PHPExcel 获得一行。在互联网上进行了大量搜索后,我只找到了一种迭代它们的方法,带有一个可选的起始行。这导致以下解决方案:

foreach ($this->objPHPExcel->getActiveSheet()->getRowIterator($rownumber) as $row) {
    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(false);

    foreach ($cellIterator as $cell) {
        echo $cell->getValue();
    }

    break;
}

but this feels a little ugly. Is there another way to do it?

但这感觉有点难看。还有另一种方法吗?

回答by Kirk Backus

Yes!

是的!

$row = $this->objPHPExcel->getActiveSheet()->getRowIterator($rownumber)->current();

$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);

foreach ($cellIterator as $cell) {
    echo $cell->getValue();
}

I don't remember if you are supposed to use nextor current. If you are getting the wrong row, use current()instead of next().

我不记得你是否应该使用nextcurrent。如果您得到错误的行,请使用current()代替next()

回答by Mark Baker

$myRow = 123;

$this->objPHPExcel->getActiveSheet()
    ->rangeToArray(
        'A' . $myRow . 
        ':' . 
        $this->objPHPExcel->getActiveSheet()->getHighestColumn() . $myRow
    );

will return the specified row as an array of cells

将指定行作为单元格数组返回