Guest User

Untitled

a guest
Sep 26th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. ## Basics
  2. Creating a new PHPExcel Object.
  3. ```
  4. $this->PHPExcel = new PHPExcel();
  5. ```
  6.  
  7. ### Working with sheets
  8.  
  9. Creating a new sheet:
  10. ```
  11. $this->activeSheet = $this->PHPExcel->createSheet();
  12. ```
  13. Getting the active Sheet:
  14. ```
  15. $this->activeSheet = $this->PHPExcel->getActiveSheet();
  16. ```
  17. Setting the active sheet:
  18. ```
  19. $this->PHPExcel->setActiveSheetIndex(2);
  20. ```
  21. Renaming a worksheet:
  22. ```
  23. $this->activeSheet->setTitle($title);
  24. ```
  25.  
  26.  
  27.  
  28.  
  29.  
  30. ## Writing to cells
  31. Text can be added to a cell using `setCellValue($colRow, $data)`
  32. `$colRow` - The column and row to write to (i.e. 'A2')
  33. `$data` - The data to write
  34.  
  35. ```
  36. $this->activeSheet
  37. ->setCellValue($colRow, $data);
  38.  
  39. $this->activeSheet
  40. ->setCellValue("B1", $data)
  41. ->setCellValue("B2", $data);
  42. ->setCellValue("B5", $data);
  43. ```
  44.  
  45. ```
  46. $this->activeSheet->setCellValueByColumnAndRow($column, $row, $data);
  47. ```
  48.  
  49. ```
  50. $this->activeSheet->setCellValueExplicit($coord, $value, $dataType);
  51. $this->activeSheet->setCellValueExplicitByColumnAndRow($col, $row, $value, $dataType);
  52. ```
  53.  
  54. #### Writing from arrays
  55. A 2-dimensional array can be written to the current sheet usng `fromArray($twoDimArray)`
  56. * `$twoDimArray` - the 2D array to be written
  57. * `$useWhenNull` - what to use if there is a null value
  58. * `$topLeftCorner` - where the top left corner should be.
  59.  
  60. ```
  61. $this->activeSheet->fromArray($sheet);
  62. $this->activeSheet->fromArray($sheet, "", $colRow);
  63. ```
  64.  
  65. Or the array can be written manually by looping through the array and calling `setCellValue`
  66. ```
  67. foreach($rows as $row => $columns) {
  68. foreach($columns as $column => $data) {
  69. $this->activeSheet->setCellValue($column.$row, $data);
  70. }
  71. }
  72. ```
  73.  
  74. ### Formatting Cells
  75. **Setting column width**
  76. A single column:
  77. ```
  78. $this->activeSheet
  79. ->getColumnDimension($colString)
  80. ->setWidth($width);
  81. ```
  82. Default width for all columns on a sheet:
  83. ```
  84. $this->activeSheet
  85. ->getDefaultColumnDimension()
  86. ->setWidth($width);
  87. ```
  88. Auto size
  89. ```
  90. $this->activeSheet
  91. ->getColumnDimension("A")
  92. ->setAutoSize(true);
  93. ```
  94. **Setting row height**
  95. A single row:
  96. ```
  97.  
  98. ```
  99. Default row height for an entire sheet:
  100. ```
  101. $this->activeSheet
  102. ->getDefaultRowDimension()
  103. ->setRowHeight($height);
  104. ```
  105.  
  106. ## Styling Cells
  107. ```
  108.  
  109.  
  110. $this->activeSheet
  111. ->getStyle("B1")
  112. ->getAlignment()
  113. ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  114.  
  115. $styleArray = array(
  116. 'font' => array(
  117. 'bold' => true,
  118. )
  119. );
  120.  
  121. $this->activeSheet
  122. ->getStyle("B1:F1")
  123. ->applyFromArray(array("font" => array( "bold" => true)));
  124. ```
  125.  
  126. ```
  127. ->getStyle("D1:D20")->getAlignment()->setWrapText(true);
  128. ```
  129.  
  130. Setting default styles for the active sheet
  131. ```
  132. $this->activeSheet
  133. ->getDefaultStyle()
  134. ->applyFromArray($this->defaultStyle);
  135. ```
  136.  
  137. ## Setting file properties
  138. ```
  139. $this->PHPExcel->getProperties()->setCreator("");
  140. $this->PHPExcel->getProperties()->setLastModifiedBy("");
  141. $this->PHPExcel->getProperties()->setTitle("");
  142. $this->PHPExcel->getProperties()->setSubject("");
  143. $this->PHPExcel->getProperties()->setDescription("..");
  144. $this->PHPExcel->getProperties()->setKeywords("");
  145. $this->PHPExcel->getProperties()->setCategory("");
  146. ```
Add Comment
Please, Sign In to add comment