DeniseMeander
DeniseMeander

Reputation: 838

How to fetch one single row of an excel sheet with Box\Spout

I'm trying out Box\Spout and rewriting some code which was formerly using PHPExcel. Iterating through rows is clear but in a few cases I need to address directly one specific row. I cannot find this in the documentation.

Something like:

$row = $sheet->getRow(8);

Upvotes: 3

Views: 2983

Answers (2)

Adrien
Adrien

Reputation: 1947

You can't access rows directly. If you need the 8th row, you'll need to read the first 8 rows... This is because Spout does not load the entire spreadsheet in memory so it reads the data row by row.

However, you can do something like this:

foreach ($reader->getSheetIterator() as $sheet) {
    foreach ($sheet->getRowIterator() as $rowIndex => $row) {
        if ($rowIndex !== 8) {
            continue;
        }

        // do something with row 8
    }
}

Upvotes: 4

delboy1978uk
delboy1978uk

Reputation: 12384

Try using the iterator's key() method:

$it = $sheet->getRowIterator();
$row = $it->key(8);

Upvotes: 1

Related Questions