| 1 |
# Creating a spreadsheet |
| 2 |
|
| 3 |
## The `Spreadsheet` class |
| 4 |
|
| 5 |
The `Spreadsheet` class is the core of PhpSpreadsheet. It contains |
| 6 |
references to the contained worksheets, document security settings and |
| 7 |
document meta data. |
| 8 |
|
| 9 |
To simplify the PhpSpreadsheet concept: the `Spreadsheet` class |
| 10 |
represents your workbook. |
| 11 |
|
| 12 |
Typically, you will create a workbook in one of two ways, either by |
| 13 |
loading it from a spreadsheet file, or creating it manually. A third |
| 14 |
option, though less commonly used, is cloning an existing workbook that |
| 15 |
has been created using one of the previous two methods. |
| 16 |
|
| 17 |
### Loading a Workbook from a file |
| 18 |
|
| 19 |
Details of the different spreadsheet formats supported, and the options |
| 20 |
available to read them into a Spreadsheet object are described fully in |
| 21 |
the [](./reading-files.mdReading Files](./reading-files.md](./reading-files.md) document. |
| 22 |
|
| 23 |
``` php |
| 24 |
$inputFileName = './sampleData/example1.xls'; |
| 25 |
|
| 26 |
/** Load $inputFileName to a Spreadsheet object **/ |
| 27 |
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName); |
| 28 |
``` |
| 29 |
|
| 30 |
### Creating a new workbook |
| 31 |
|
| 32 |
If you want to create a new workbook, rather than load one from file, |
| 33 |
then you simply need to instantiate it as a new Spreadsheet object. |
| 34 |
|
| 35 |
``` php |
| 36 |
/** Create a new Spreadsheet Object **/ |
| 37 |
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); |
| 38 |
``` |
| 39 |
|
| 40 |
A new workbook will always be created with a single worksheet. |
| 41 |
|
| 42 |
## Clearing a Workbook from memory |
| 43 |
|
| 44 |
The PhpSpreadsheet object contains cyclic references (e.g. the workbook |
| 45 |
is linked to the worksheets, and the worksheets are linked to their |
| 46 |
parent workbook) which cause problems when PHP tries to clear the |
| 47 |
objects from memory when they are `unset()`, or at the end of a function |
| 48 |
when they are in local scope. The result of this is "memory leaks", |
| 49 |
which can easily use a large amount of PHP's limited memory. |
| 50 |
|
| 51 |
This can only be resolved manually: if you need to unset a workbook, |
| 52 |
then you also need to "break" these cyclic references before doing so. |
| 53 |
PhpSpreadsheet provides the `disconnectWorksheets()` method for this |
| 54 |
purpose. |
| 55 |
|
| 56 |
``` php |
| 57 |
$spreadsheet->disconnectWorksheets(); |
| 58 |
unset($spreadsheet); |
| 59 |
``` |
| 60 |
|