For God so loved the world, that He gave His only begotten Son, that all who believe in Him should not perish but have everlasting life
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.

94 lines
4.7 KiB

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