getting started

This commit is contained in:
madflow 2018-02-08 14:04:52 +01:00
parent 21e0e9e6b1
commit a9d8146ce9

View File

@ -8,7 +8,7 @@ This guide will help you install {{ site.spout_html }} and teach you how to use
## Requirements
* PHP version 5.4.0 or higher
* PHP version 5.6.0 or higher
* PHP extension `php_zip` enabled
* PHP extension `php_xmlreader` enabled
@ -34,7 +34,7 @@ If you can't use Composer, no worries! You can still install {{ site.spout_html
2. Extract the downloaded content into your project.
3. Add this code to the top controller (e.g. index.php) or wherever it may be more appropriate:
```php?start_inline=1
```php
// don't forget to change the path!
require_once '[PATH/TO]/src/Spout/Autoloader/autoload.php';
```
@ -46,8 +46,8 @@ require_once '[PATH/TO]/src/Spout/Autoloader/autoload.php';
Regardless of the file type, the interface to read a file is always the same:
```php?start_inline=1
use Box\Spout\Reader\ReaderFactory;
```php
use Box\Spout\Reader\Common\Creator\EntityFactory as ReaderFactory;
use Box\Spout\Common\Type;
$reader = ReaderFactory::create(Type::XLSX); // for XLSX files
@ -56,7 +56,9 @@ $reader = ReaderFactory::create(Type::XLSX); // for XLSX files
$reader->open($filePath);
/** @var $sheet Spout/Reader/XLSX/Sheet */
foreach ($reader->getSheetIterator() as $sheet) {
/** @var $row Spout/Common/Entity/Row */
foreach ($sheet->getRowIterator() as $row) {
// do stuff with the row
}
@ -71,19 +73,23 @@ If there are multiple sheets in the file, the reader will read all of them seque
As with the reader, there is one common interface to write data to a file:
```php?start_inline=1
use Box\Spout\Writer\WriterFactory;
```php
use Box\Spout\Writer\Common\Creator\EntityFactory as WriterFactory;
use Box\Spout\Common\Type;
$writer = WriterFactory::create(Type::XLSX); // for XLSX files
$writer = WriterFactory::createWriter(Type::XLSX); // for XLSX files
//$writer = WriterFactory::create(Type::CSV); // for CSV files
//$writer = WriterFactory::create(Type::ODS); // for ODS files
$writer->openToFile($filePath); // write data to a file or to a PHP stream
//$writer->openToBrowser($fileName); // stream data directly to the browser
$writer->addRow($singleRow); // add a row at a time
$writer->addRows($multipleRows); // add multiple rows at a time
/** @var $row Spout/Common/Entity/Row */
$row = WriterFactory::createRow();
$row->addCell(WriterFactory::createCell('I am a cell text'));
$writer->addRow($row); // add a row at a time
$writer->addRows([$row]); // add multiple rows at a time
$writer->close();
```