酒店预订平台
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.

worksheets.md 4.6 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Worksheets
  2. A worksheet is a collection of cells, formulae, images, graphs, etc. It
  3. holds all data necessary to represent a spreadsheet worksheet.
  4. When you load a workbook from a spreadsheet file, it will be loaded with
  5. all its existing worksheets (unless you specified that only certain
  6. sheets should be loaded). When you load from non-spreadsheet files (such
  7. as a CSV or HTML file) or from spreadsheet formats that don't identify
  8. worksheets by name (such as SYLK), then a single worksheet called
  9. "WorkSheet1" will be created containing the data from that file.
  10. When you instantiate a new workbook, PhpSpreadsheet will create it with
  11. a single worksheet called "WorkSheet1".
  12. The `getSheetCount()` method will tell you the number of worksheets in
  13. the workbook; while the `getSheetNames()` method will return a list of
  14. all worksheets in the workbook, indexed by the order in which their
  15. "tabs" would appear when opened in MS Excel (or other appropriate
  16. Spreadsheet program).
  17. Individual worksheets can be accessed by name, or by their index
  18. position in the workbook. The index position represents the order that
  19. each worksheet "tab" is shown when the workbook is opened in MS Excel
  20. (or other appropriate Spreadsheet program). To access a sheet by its
  21. index, use the `getSheet()` method.
  22. ``` php
  23. // Get the second sheet in the workbook
  24. // Note that sheets are indexed from 0
  25. $spreadsheet->getSheet(1);
  26. ```
  27. Methods also exist allowing you to reorder the worksheets in the
  28. workbook.
  29. To access a sheet by name, use the `getSheetByName()` method, specifying
  30. the name of the worksheet that you want to access.
  31. ``` php
  32. // Retrieve the worksheet called 'Worksheet 1'
  33. $spreadsheet->getSheetByName('Worksheet 1');
  34. ```
  35. Alternatively, one worksheet is always the currently active worksheet,
  36. and you can access that directly. The currently active worksheet is the
  37. one that will be active when the workbook is opened in MS Excel (or
  38. other appropriate Spreadsheet program).
  39. ``` php
  40. // Retrieve the current active worksheet
  41. $spreadsheet->getActiveSheet();
  42. ```
  43. You can change the currently active sheet by index or by name using the
  44. `setActiveSheetIndex()` and `setActiveSheetIndexByName()` methods.
  45. ## Adding a new Worksheet
  46. You can add a new worksheet to the workbook using the `createSheet()`
  47. method of the `Spreadsheet` object. By default, this will be created as
  48. a new "last" sheet; but you can also specify an index position as an
  49. argument, and the worksheet will be inserted at that position, shuffling
  50. all subsequent worksheets in the collection down a place.
  51. ``` php
  52. $spreadsheet->createSheet();
  53. ```
  54. A new worksheet created using this method will be called
  55. `Worksheet<n>` where `<n>` is the lowest number possible to
  56. guarantee that the title is unique.
  57. Alternatively, you can instantiate a new worksheet (setting the title to
  58. whatever you choose) and then insert it into your workbook using the
  59. `addSheet()` method.
  60. ``` php
  61. // Create a new worksheet called "My Data"
  62. $myWorkSheet = new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet($spreadsheet, 'My Data');
  63. // Attach the "My Data" worksheet as the first worksheet in the Spreadsheet object
  64. $spreadsheet->addSheet($myWorkSheet, 0);
  65. ```
  66. If you don't specify an index position as the second argument, then the
  67. new worksheet will be added after the last existing worksheet.
  68. ## Copying Worksheets
  69. Sheets within the same workbook can be copied by creating a clone of the
  70. worksheet you wish to copy, and then using the `addSheet()` method to
  71. insert the clone into the workbook.
  72. ``` php
  73. $clonedWorksheet = clone $spreadsheet->getSheetByName('Worksheet 1');
  74. $clonedWorksheet->setTitle('Copy of Worksheet 1');
  75. $spreadsheet->addSheet($clonedWorksheet);
  76. ```
  77. You can also copy worksheets from one workbook to another, though this
  78. is more complex as PhpSpreadsheet also has to replicate the styling
  79. between the two workbooks. The `addExternalSheet()` method is provided for
  80. this purpose.
  81. $clonedWorksheet = clone $spreadsheet1->getSheetByName('Worksheet 1');
  82. $spreadsheet->addExternalSheet($clonedWorksheet);
  83. In both cases, it is the developer's responsibility to ensure that
  84. worksheet names are not duplicated. PhpSpreadsheet will throw an
  85. exception if you attempt to copy worksheets that will result in a
  86. duplicate name.
  87. ## Removing a Worksheet
  88. You can delete a worksheet from a workbook, identified by its index
  89. position, using the `removeSheetByIndex()` method
  90. ``` php
  91. $sheetIndex = $spreadsheet->getIndex(
  92. $spreadsheet->getSheetByName('Worksheet 1')
  93. );
  94. $spreadsheet->removeSheetByIndex($sheetIndex);
  95. ```
  96. If the currently active worksheet is deleted, then the sheet at the
  97. previous index position will become the currently active sheet.