Add helper functions to create specific readers and writers
Removed the `ReaderEntityFactory::createReader(Type)` method and replaced it by 3 methods: - `ReaderEntityFactory::createCSVReader()` - `ReaderEntityFactory::createXLSXReader()` - `ReaderEntityFactory::createODSReader()` This has the advantage of enabling autocomplete in the IDE, as the return type is no longer the interface but the concrete type. Since readers may expose different options, this is pretty useful. Similarly, removed the `WriterEntityFactory::createWriter(Type)` method and replaced it by 3 methods: - `WriterEntityFactory::createCSVWriter()` - `WriterEntityFactory::createXLSXWriter()` - `WriterEntityFactory::createODSWriter()` Since this is a breaking change, I also updated the Upgrade guide. Finally, the doc is up to date too.
This commit is contained in:
1 parent
6104d41857
commit
40ee386edd
30 files changed
+235
-275
No files matched your search
@@ -44,9 +44,8 @@ It is possible to change the behavior of the writers when the maximum number of
|
||||
```php
|
||||
|
||||
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
|
||||
use Box\Spout\Common\Type;
|
||||
|
||||
$writer = WriterEntityFactory::createWriter(Type::ODS);
|
||||
$writer = WriterEntityFactory::createODSWriter();
|
||||
$writer->setShouldCreateNewSheetsAutomatically(true); // default value
|
||||
$writer->setShouldCreateNewSheetsAutomatically(false); // will stop writing new data when limit is reached
|
||||
```
|
||||
@@ -57,9 +56,8 @@ Processing XLSX and ODS files requires temporary files to be created. By default
|
||||
|
||||
```php
|
||||
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
|
||||
use Box\Spout\Common\Type;
|
||||
|
||||
$writer = WriterEntityFactory::createWriter(Type::XLSX);
|
||||
$writer = WriterEntityFactory::createXLSXWriter();
|
||||
$writer->setTempFolder($customTempFolderPath);
|
||||
```
|
||||
|
||||
@@ -73,9 +71,8 @@ In order to keep the memory usage really low, {{ site.spout_html }} does not de-
|
||||
|
||||
```php
|
||||
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
|
||||
use Box\Spout\Common\Type;
|
||||
|
||||
$writer = WriterEntityFactory::createWriter(Type::XLSX);
|
||||
$writer = WriterEntityFactory::createXLSXWriter();
|
||||
$writer->setShouldUseInlineStrings(true); // default (and recommended) value
|
||||
$writer->setShouldUseInlineStrings(false); // will use shared strings
|
||||
```
|
||||
@@ -91,9 +88,8 @@ It is possible to change this behavior and have a formatted date returned instea
|
||||
|
||||
```php
|
||||
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
|
||||
use Box\Spout\Common\Type;
|
||||
|
||||
$reader = ReaderEntityFactory::createReader(Type::XLSX);
|
||||
$reader = ReaderEntityFactory::createXLSXReader();
|
||||
$reader->setShouldFormatDates(false); // default value
|
||||
$reader->setShouldFormatDates(true); // will return formatted dates
|
||||
```
|
||||
@@ -137,12 +133,11 @@ For fonts and alignments, {{ site.spout_html }} does not support all the possibl
|
||||
It is possible to apply some formatting options to a row. In this case, all cells of the row will have the same style:
|
||||
|
||||
```php
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
|
||||
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
|
||||
use Box\Spout\Common\Entity\Style\Color;
|
||||
|
||||
$writer = WriterEntityFactory::createWriter(Type::XLSX);
|
||||
$writer = WriterEntityFactory::createXLSXWriter();
|
||||
$writer->openToFile($filePath);
|
||||
|
||||
/** Create a style with the StyleBuilder */
|
||||
@@ -165,7 +160,6 @@ $writer->close();
|
||||
Adding borders to a row requires a ```Border``` object.
|
||||
|
||||
```php
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\Common\Entity\Style\Border;
|
||||
use Box\Spout\Writer\Common\Creator\Style\BorderBuilder;
|
||||
use Box\Spout\Common\Entity\Style\Color;
|
||||
@@ -180,7 +174,7 @@ $style = (new StyleBuilder())
|
||||
->setBorder($border)
|
||||
->build();
|
||||
|
||||
$writer = WriterEntityFactory::createWriter(Type::XLSX);
|
||||
$writer = WriterEntityFactory::createXLSXWriter();
|
||||
$writer->openToFile($filePath);
|
||||
|
||||
$cells = WriterEntityFactory::createCell('Border Bottom Green Thin Dashed');
|
||||
@@ -202,7 +196,6 @@ The styles applied to a specific cell will override any parent styles if present
|
||||
Example:
|
||||
|
||||
```php
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\Common\Entity\Style\Color;
|
||||
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
|
||||
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
|
||||
@@ -211,7 +204,7 @@ $defaultStyle = (new StyleBuilder())
|
||||
->setFontSize(8)
|
||||
->build();
|
||||
|
||||
$writer = WriterEntityFactory::createWriter(Type::XLSX);
|
||||
$writer = WriterEntityFactory::createXLSXWriter();
|
||||
$writer->setDefaultRowStyle($defaultStyle)
|
||||
->openToFile($filePath);
|
||||
|
||||
@@ -252,7 +245,7 @@ $defaultStyle = (new StyleBuilder())
|
||||
->setFontSize(11)
|
||||
->build();
|
||||
|
||||
$writer = WriterEntityFactory::createWriter(Type::XLSX);
|
||||
$writer = WriterEntityFactory::createXLSXWriter();
|
||||
$writer->setDefaultRowStyle($defaultStyle)
|
||||
->openToFile($filePath);
|
||||
```
|
||||
|
||||
@@ -68,16 +68,14 @@ $reader->close();
|
||||
If there are multiple sheets in the file, the reader will read all of them sequentially.
|
||||
|
||||
|
||||
Note that {{ site.spout_html }} guesses the reader type based on the file extension. If the extension is not standard (`.csv`, `.ods`, `.xlsx` _- lower/uppercase_), the reader type needs to be specified:
|
||||
Note that {{ site.spout_html }} guesses the reader type based on the file extension. If the extension is not standard (`.csv`, `.ods`, `.xlsx` _- lower/uppercase_), a specific reader can be created directly:
|
||||
|
||||
```php
|
||||
|
||||
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
|
||||
use Box\Spout\Common\Type;
|
||||
|
||||
$reader = ReaderEntityFactory::createReader(Type::XLSX); // for XLSX files
|
||||
// $reader = ReaderEntityFactory::createReader(Type::ODS); // for ODS files
|
||||
// $reader = ReaderEntityFactory::createReader(Type::CSV); // for CSV files
|
||||
$reader = ReaderEntityFactory::createXLSXReader();
|
||||
// $reader = ReaderEntityFactory::createODSReader();
|
||||
// $reader = ReaderEntityFactory::createCSVReader();
|
||||
```
|
||||
|
||||
### Writer
|
||||
@@ -124,9 +122,9 @@ Similar to the reader, if the file extension of the file to be written is not st
|
||||
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
|
||||
use Box\Spout\Common\Entity\Row;
|
||||
|
||||
$writer = WriterEntityFactory::createWriter(Type::XLSX);
|
||||
// $writer = WriterEntityFactory::createWriter(Type::ODS);
|
||||
// $writer = WriterEntityFactory::createWriter(Type::CSV);
|
||||
$writer = WriterEntityFactory::createXLSXWriter();
|
||||
// $writer = WriterEntityFactory::createODSWriter();
|
||||
// $writer = WriterEntityFactory::createCSVWriter();
|
||||
```
|
||||
|
||||
For XLSX and ODS files, the number of rows per sheet is limited to *1,048,576*. By default, once this limit is reached, the writer will automatically create a new sheet and continue writing data into it.
|
||||
|
||||
@@ -12,10 +12,9 @@ Even though a spreadsheet contains multiple sheets, you may be interested in rea
|
||||
```php
|
||||
<?php
|
||||
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
|
||||
|
||||
$reader = ReaderEntityFactory::createReader(Type::XLSX);
|
||||
$reader = ReaderEntityFactory::createXLSXReader();
|
||||
$reader->open($filePath);
|
||||
|
||||
foreach ($reader->getSheetIterator() as $sheet) {
|
||||
@@ -36,10 +35,9 @@ $reader->close();
|
||||
```php
|
||||
<?php
|
||||
|
||||
use Box\Spout\Common\Type;
|
||||
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
|
||||
|
||||
$reader = ReaderEntityFactory::createReader(Type::XLSX);
|
||||
$reader = ReaderEntityFactory::createXLSXReader();
|
||||
$reader->open($filePath);
|
||||
|
||||
foreach ($reader->getSheetIterator() as $sheet) {
|
||||
|
||||
@@ -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 = ReaderEntityFactory::createReader(Type::XLSX);
|
||||
$reader = ReaderEntityFactory::createXLSXReader();
|
||||
$reader->open($filePath);
|
||||
|
||||
$i = 0;
|
||||
|
||||
Reference in new issue
Block a user