You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

creating-spreadsheet.md 2.0 KiB

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