PhpSpreadsheet/docs/ReadingSpreadsheetFiles/03-Loading-a-Spreadsheet.md

22 lines
1.5 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
## Loading a Spreadsheet File
2016-11-27 15:51:44 +00:00
The simplest way to load a workbook file is to let PhpSpreadsheet's IO Factory identify the file type and load it, calling the static load() method of the \PhpOffice\PhpSpreadsheet\IOFactory class.
2013-05-20 11:21:56 +00:00
```php
$inputFileName = './sampleData/example1.xls';
2016-11-27 15:51:44 +00:00
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);
2013-05-20 11:21:56 +00:00
```
> See Examples/Reader/exampleReader01.php for a working example of this code.
2016-11-27 15:51:44 +00:00
The load() method will attempt to identify the file type, and instantiate a loader for that file type; using it to load the file and store the data and any formatting in a `Spreadsheet` object.
2013-05-20 11:21:56 +00:00
2016-10-06 11:49:41 +00:00
The method makes an initial guess at the loader to instantiate based on the file extension; but will test the file before actually executing the load: so if (for example) the file is actually a CSV file or contains HTML markup, but that has been given a .xls extension (quite a common practise), it will reject the Xls loader that it would normally use for a .xls file; and test the file using the other loaders until it finds the appropriate loader, and then use that to read the file.
2013-05-20 11:21:56 +00:00
2016-11-27 15:51:44 +00:00
While easy to implement in your code, and you don't need to worry about the file type; this isn't the most efficient method to load a file; and it lacks the flexibility to configure the loader in any way before actually reading the file into a `Spreadsheet` object.
2013-05-20 11:21:56 +00:00