spout/docs/_pages/guides/3-read-data-from-specific-sheet.md
2017-11-26 20:02:20 +01:00

1.2 KiB

layout title category permalink
page Read data from a specific sheet only guide /guides/read-data-from-specific-sheet/

Even though a spreadsheet contains multiple sheets, you may be interested in reading only one of them and skip the other ones. Here is how you can do it with Spout:

  • If you know the name of the sheet
$reader = ReaderFactory::create(Type:XLSX);
$reader->open($filePath);

foreach ($reader->getSheetIterator() as $sheet) {
    // only read data from "summary" sheet
    if ($sheet->getName() === 'summary') {
        foreach ($sheet->getRowIterator() as $row) {
            // do something with the row
        }
        break; // no need to read more sheets
    }
}

$reader->close();
  • If you know the position of the sheet
$reader = ReaderFactory::create(Type:XLSX);
$reader->open($filePath);

foreach ($reader->getSheetIterator() as $sheet) {
    // only read data from 3rd sheet
    if ($sheet->getIndex() === 2) { // index is 0-based
        foreach ($sheet->getRowIterator() as $row) {
            // do something with the row
        }
        break; // no need to read more sheets
    }
}

$reader->close();