PhpSpreadsheet/docs/ReadingSpreadsheetFiles/04-Loading-with-a-Reader.md

57 lines
2.8 KiB
Markdown
Raw Normal View History

2016-11-27 15:51:44 +00:00
# PhpSpreadsheet User Documentation Reading Spreadsheet Files
2013-05-20 11:21:56 +00:00
## Creating a Reader and Loading a Spreadsheet File
2016-11-27 15:51:44 +00:00
If you know the file type of the spreadsheet file that you need to load, you can instantiate a new reader object for that file type, then use the reader's load() method to read the file to a `Spreadsheet` object. It is possible to instantiate the reader objects for each of the different supported filetype by name. However, you may get unpredictable results if the file isn't of the right type (e.g. it is a CSV with an extension of .xls), although this type of exception should normally be trapped.
2013-05-20 11:21:56 +00:00
```php
$inputFileName = './sampleData/example1.xls';
2016-10-06 11:49:41 +00:00
/** Create a new Xls Reader **/
2016-11-27 15:51:44 +00:00
$objReader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
// $objReader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
// $objReader = new \PhpOffice\PhpSpreadsheet\Reader\Excel2003XML();
// $objReader = new \PhpOffice\PhpSpreadsheet\Reader\Ods();
// $objReader = new \PhpOffice\PhpSpreadsheet\Reader\SYLK();
// $objReader = new \PhpOffice\PhpSpreadsheet\Reader\Gnumeric();
// $objReader = new \PhpOffice\PhpSpreadsheet\Reader\CSV();
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $objReader->load($inputFileName);
2013-05-20 11:21:56 +00:00
```
> See Examples/Reader/exampleReader02.php for a working example of this code.
Alternatively, you can use the IO Factory's createReader() method to instantiate the reader object for you, simply telling it the file type of the reader that you want instantiating.
```php
2016-10-06 11:49:41 +00:00
$inputFileType = 'Xls';
2016-10-06 11:39:10 +00:00
// $inputFileType = 'Xlsx';
2013-05-20 11:21:56 +00:00
// $inputFileType = 'Excel2003XML';
// $inputFileType = 'Ods';
2013-05-20 11:21:56 +00:00
// $inputFileType = 'SYLK';
// $inputFileType = 'Gnumeric';
// $inputFileType = 'CSV';
$inputFileName = './sampleData/example1.xls';
/** Create a new Reader of the type defined in $inputFileType **/
2016-11-27 15:51:44 +00:00
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $objReader->load($inputFileName);
2013-05-20 11:21:56 +00:00
```
> See Examples/Reader/exampleReader03.php for a working example of this code.
If you're uncertain of the filetype, you can use the IO Factory's identify() method to identify the reader that you need, before using the createReader() method to instantiate the reader object.
```php
$inputFileName = './sampleData/example1.xls';
/** Identify the type of $inputFileName **/
2016-11-27 15:51:44 +00:00
$inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($inputFileName);
2013-05-20 11:21:56 +00:00
/** Create a new Reader of the type that has been identified **/
2016-11-27 15:51:44 +00:00
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $objReader->load($inputFileName);
2013-05-20 11:21:56 +00:00
```
> See Examples/Reader/exampleReader04.php for a working example of this code.