Update documentation for 3.0
This commit is contained in:
1 parent
4a9d0398ad
commit
4260c46b11
10 files changed
+190
-101
No files matched your search
@@ -14,20 +14,19 @@ In order to avoid memory issues when dealing with large spreadsheets, {{ site.sp
|
||||
```php
|
||||
<?php
|
||||
|
||||
use Box\Spout\Reader\ReaderFactory;
|
||||
use Box\Spout\Writer\WriterFactory;
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
|
||||
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
|
||||
|
||||
$existingFilePath = 'path/to/orders.xlsx';
|
||||
$newFilePath = 'path/to/new-orders.xlsx';
|
||||
|
||||
// we need a reader to read the existing file...
|
||||
$reader = ReaderFactory::create(Type::XLSX);
|
||||
$reader->open($existingFilePath);
|
||||
$reader = ReaderEntityFactory::createReaderFromFile($existingFilePath);
|
||||
$reader->setShouldFormatDates(true); // this is to be able to copy dates
|
||||
$reader->open($existingFilePath);
|
||||
|
||||
// ... and a writer to create the new file
|
||||
$writer = WriterFactory::create(Type::XLSX);
|
||||
$writer = WriterEntityFactory::createWriterFromFile($newFilePath);
|
||||
$writer->openToFile($newFilePath);
|
||||
|
||||
// let's read the entire spreadsheet...
|
||||
@@ -45,7 +44,9 @@ foreach ($reader->getSheetIterator() as $sheetIndex => $sheet) {
|
||||
|
||||
// At this point, the new spreadsheet contains the same data as the existing one.
|
||||
// So let's add the new data:
|
||||
$writer->addRow(['2015-12-25', 'Christmas gift', 29, 'USD']);
|
||||
$writer->addRow(
|
||||
WriterEntityFactory::createRowFromArray(['2015-12-25', 'Christmas gift', 29, 'USD'])
|
||||
);
|
||||
|
||||
$reader->close();
|
||||
$writer->close();
|
||||
|
||||
@@ -27,20 +27,19 @@ We'd like to update the missing album for "Yellow Submarine", remove the Bob Mar
|
||||
```php
|
||||
<?php
|
||||
|
||||
use Box\Spout\Reader\ReaderFactory;
|
||||
use Box\Spout\Writer\WriterFactory;
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
|
||||
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
|
||||
|
||||
$existingFilePath = '/path/to/my-music.ods';
|
||||
$newFilePath = '/path/to/my-new-music.ods';
|
||||
|
||||
// we need a reader to read the existing file...
|
||||
$reader = ReaderFactory::create(Type::ODS);
|
||||
$reader = ReaderEntityFactory::createReaderFromFile($existingFilePath);
|
||||
$reader->open($existingFilePath);
|
||||
$reader->setShouldFormatDates(true); // this is to be able to copy dates
|
||||
|
||||
// ... and a writer to create the new file
|
||||
$writer = WriterFactory::create(Type::ODS);
|
||||
$writer = WriterEntityFactory::createWriterFromFile($newFilePath);
|
||||
$writer->openToFile($newFilePath);
|
||||
|
||||
// let's read the entire spreadsheet
|
||||
@@ -51,12 +50,12 @@ foreach ($reader->getSheetIterator() as $sheetIndex => $sheet) {
|
||||
}
|
||||
|
||||
foreach ($sheet->getRowIterator() as $rowIndex => $row) {
|
||||
$songTitle = $row[0];
|
||||
$artist = $row[1];
|
||||
$songTitle = $row->getCellAtIndex(0);
|
||||
$artist = $row->getCellAtIndex(1);
|
||||
|
||||
// Change the album name for "Yellow Submarine"
|
||||
if ($songTitle === 'Yellow Submarine') {
|
||||
$row[2] = 'The White Album';
|
||||
$row->setCellAtIndex(WriterEntityFactory::createCell('The White Album'), 2);
|
||||
}
|
||||
|
||||
// skip Bob Marley's songs
|
||||
@@ -69,7 +68,9 @@ foreach ($reader->getSheetIterator() as $sheetIndex => $sheet) {
|
||||
|
||||
// insert new song at the right position, between the 3rd and 4th rows
|
||||
if ($rowIndex === 3) {
|
||||
$writer->addRow(['Hotel California', 'The Eagles', 'Hotel California', 1976]);
|
||||
$writer->addRow(
|
||||
WriterEntityFactory::createRowFromArray(['Hotel California', 'The Eagles', 'Hotel California', 1976])
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,12 +5,17 @@ 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:
|
||||
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 {{ site.spout_html }}:
|
||||
|
||||
* If you know the name of the sheet
|
||||
|
||||
```php?start_inline=1
|
||||
$reader = ReaderFactory::create(Type:XLSX);
|
||||
```php
|
||||
<?php
|
||||
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
|
||||
|
||||
$reader = ReaderEntityFactory::createReader(Type::XLSX);
|
||||
$reader->open($filePath);
|
||||
|
||||
foreach ($reader->getSheetIterator() as $sheet) {
|
||||
@@ -28,8 +33,13 @@ $reader->close();
|
||||
|
||||
* If you know the position of the sheet
|
||||
|
||||
```php?start_inline=1
|
||||
$reader = ReaderFactory::create(Type:XLSX);
|
||||
```php
|
||||
<?php
|
||||
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
|
||||
|
||||
$reader = ReaderEntityFactory::createReader(Type::XLSX);
|
||||
$reader->open($filePath);
|
||||
|
||||
foreach ($reader->getSheetIterator() as $sheet) {
|
||||
|
||||
@@ -27,7 +27,7 @@ class MyRegularController extends Controller
|
||||
// before it can be sent to the browser.
|
||||
$content = '';
|
||||
|
||||
$reader = ReaderFactory::create(Type::XLSX);
|
||||
$reader = ReaderEntityFactory::createReaderFromFile($filePath);
|
||||
$reader->open($filePath);
|
||||
|
||||
foreach ($reader->getSheetIterator() as $sheet) {
|
||||
@@ -36,7 +36,7 @@ class MyRegularController extends Controller
|
||||
$content .= '<tr>';
|
||||
$content .= implode(array_map(function($cell) {
|
||||
return '<td>' . $cell . '</td>';
|
||||
}, $row));
|
||||
}, $row->getCells()));
|
||||
$content .= '</tr>';
|
||||
}
|
||||
$content .= '</table><br>';
|
||||
@@ -77,7 +77,7 @@ class MyStreamController extends Controller
|
||||
// a callback function to retrieve data chunks.
|
||||
$response->setCallback(function() use ($filePath) {
|
||||
// Same code goes inside the callback.
|
||||
$reader = ReaderFactory::create(Type::XLSX);
|
||||
$reader = ReaderEntityFactory::createReader(Type::XLSX);
|
||||
$reader->open($filePath);
|
||||
|
||||
$i = 0;
|
||||
@@ -89,7 +89,7 @@ class MyStreamController extends Controller
|
||||
echo '<tr>';
|
||||
echo implode(array_map(function($cell) {
|
||||
return '<td>' . $cell . '</td>';
|
||||
}, $row));
|
||||
}, $row->getCells()));
|
||||
echo '</tr>';
|
||||
|
||||
$i++;
|
||||
|
||||
Reference in new issue
Block a user