To prepare the migration to 3.0, we need to change the location where the documentation is generated from. Having a gh-pages branch makes it hard to synchronize the code and the docs. Having a "docs" folder in the repo itself simplifies this.
47 lines
1.2 KiB
Markdown
47 lines
1.2 KiB
Markdown
---
|
|
layout: page
|
|
title: "Read data from a specific sheet only"
|
|
category: guide
|
|
permalink: /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
|
|
|
|
```php?start_inline=1
|
|
$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
|
|
|
|
```php?start_inline=1
|
|
$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();
|
|
```
|