Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Just get one row from PHPExcel

Tags:

php

phpexcel

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:

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?

like image 775
Bart Friederichs Avatar asked Jul 30 '13 15:07

Bart Friederichs


2 Answers

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 next or current. If you are getting the wrong row, use current() instead of next().

like image 200
Kirk Backus Avatar answered Oct 21 '22 23:10

Kirk Backus


$myRow = 123;

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

will return the specified row as an array of cells

like image 45
Mark Baker Avatar answered Oct 21 '22 22:10

Mark Baker