Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. <?php
  2.  
  3. class Php_ExcelWriter implements ExcelWriterInterface
  4. {
  5. protected $spreadsheet;
  6. protected $writer;
  7. protected $templateSheet = -1;
  8.  
  9. public function __construct()
  10. {
  11. $this->spreadsheet = new \ExcelBook('User', 'key', true);
  12. }
  13.  
  14. public function setTemplate($path)
  15. {
  16. $this->spreadsheet->loadFile($path);
  17. $this->currentSheet = $this->spreadsheet->getSheet(0);
  18. }
  19.  
  20. public function setTemplateSheet($index)
  21. {
  22. $this->templateSheet = $index;
  23. }
  24.  
  25. public function selectSheet($index)
  26. {
  27. $this->currentSheet = $this->spreadsheet->getSheet($index);
  28. $this->spreadsheet->activeSheet($index);
  29. }
  30.  
  31. public function setCreator($creator)
  32. {
  33. //TODO
  34. // $properties = $this->spreadsheet->getProperties();
  35. // $properties->setCreator($creator);
  36. // $properties->setLastModifiedBy("Maarten Balliauw");
  37. // $properties->setTitle("Office 2007 XLSX Test Document");
  38. // $properties->setSubject("Office 2007 XLSX Test Document");
  39. // $properties->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
  40. // $properties->setKeywords("office 2007 openxml php");
  41. // $properties->setCategory("Test result file");
  42. }
  43.  
  44. public function setCellValue($cell, $value)
  45. {
  46. $pos = $this->currentSheet->addrToRowCol($cell);
  47. $this->currentSheet->write($pos['row'], $pos['column'], $value);
  48. }
  49.  
  50. public function removeSheet($index)
  51. {
  52. $this->spreadsheet->deleteSheet($index);
  53. }
  54.  
  55. public function addImage($path, $cell, $name, $height, $width)
  56. {
  57. $imageId = $this->spreadsheet->addPictureFromFile($path);
  58. $pos = $this->currentSheet->addrToRowCol($cell);
  59. $this->currentSheet->addPictureDim($pos['row'], $pos['column'], $imageId, $width, $height);
  60. }
  61.  
  62. public function createNewFromTemplate()
  63. {
  64. if ($this->templateSheet > -1){
  65. $this->currentSheet = $this->spreadsheet->copySheet('', $this->templateSheet);
  66. $sheetCount = $this->spreadsheet->sheetCount();
  67. $this->spreadsheet->insertSheet($sheetCount, '', $this->currentSheet);
  68. }
  69. else{
  70. $this->spreadsheet->addSheet('');
  71. }
  72. }
  73.  
  74. public function insertRowsBefore($row, $number = 1)
  75. {
  76. $firstRow = $row - 1;
  77. $lastRow = $number + $row - 2;
  78. $retval = $this->currentSheet->insertRow($firstRow, $lastRow);
  79.  
  80. $formats = array();
  81. for ($i = 0; $i < 20; $i++) {
  82. $formats[] = $this->currentSheet->cellFormat($firstRow - 1, $i);
  83. }
  84.  
  85. for ($i = $firstRow; $i < $lastRow + 1; $i++){
  86. for ($j = 0; $j < 20; $j++) {
  87. $this->currentSheet->setCellFormat($i, $j, $formats[$j]);
  88. }
  89. }
  90. }
  91.  
  92. public function fromArray($data, $cell)
  93. {
  94. $pos = $this->currentSheet->addrToRowCol($cell);
  95. $length = count($data);
  96. for ($i = 0; $i < $length; $i++) {
  97. $row = $data[$i];
  98. if ($pos['column'] > 0){
  99. for ($j = 0; $j < $pos['column']; $j++) {
  100. array_unshift($row, '');
  101. }
  102. }
  103. $this->currentSheet->writeRow($pos['row'] + $i, $row);
  104. }
  105. }
  106.  
  107. public function setCellsCurrency($cells, $currencySymbol = null, $before = true)
  108. {
  109. // Setup a new format
  110. $formatCode = '#,##0.00';
  111. if (!is_null($currencySymbol)){
  112. if ($before){
  113. $formatCode = $currencySymbol . $formatCode;
  114. }
  115. else{
  116. $formatCode .= ' ' . $currencySymbol;
  117. }
  118. }
  119.  
  120. // Create the object
  121. $formatId = $this->spreadsheet->addCustomFormat($formatCode);
  122. if (strpos($cells, ':') !== false){
  123. $cellRange = explode(':', $cells);
  124. $firstRange = $this->currentSheet->addrToRowCol($cellRange[0]);
  125. $secondRange = $this->currentSheet->addrToRowCol($cellRange[1]);
  126. // So we don't lose formatting, base this on the format of the first cell.
  127. $format = $this->currentSheet->cellFormat($firstRange['row'], $firstRange['column']);
  128. $format->numberFormat($formatId);
  129.  
  130. // Loop through the range.
  131. for ($i = $firstRange['row']; $i < $secondRange['row'] + 1; $i++) {
  132. $this->currentSheet->setCellFormat($i, $firstRange['column'], $format);
  133. }
  134. }
  135. else{
  136. $pos = $this->currentSheet->addrToRowCol($cells);
  137. $format = $this->currentSheet->cellFormat($pos['row'], $pos['column']);
  138. $format->numberFormat($formatId);
  139. $this->currentSheet->setCellFormat($pos['row'], $pos['column'], $format);
  140. }
  141. }
  142.  
  143. public function setCellsPercent($cells)
  144. {
  145. if (strpos($cells, ':') !== false){
  146. $cellRange = explode(':', $cells);
  147. $firstRange = $this->currentSheet->addrToRowCol($cellRange[0]);
  148. $secondRange = $this->currentSheet->addrToRowCol($cellRange[1]);
  149. // So we don't lose formatting, base this on the format of the first cell.
  150. $format = $this->currentSheet->cellFormat($firstRange['row'], $firstRange['column']);
  151. $format->numberFormat(\ExcelFormat::NUMFORMAT_PERCENT);
  152.  
  153. // Loop through the range.
  154. for ($i = $firstRange['row']; $i < $secondRange['row'] + 1; $i++) {
  155. $this->currentSheet->setCellFormat($i, $firstRange['column'], $format);
  156. }
  157. }
  158. else{
  159. $pos = $this->currentSheet->addrToRowCol($cellRange[0]);
  160. $format = $this->currentSheet->cellFormat($pos['row'], $pos['column']);
  161. $format->numberFormat($formatId);
  162. $this->currentSheet->setCellFormat($pos['row'], $pos['column'], $format);
  163. }
  164. }
  165.  
  166. public function setSheetTitle($title)
  167. {
  168. $this->currentSheet->setName($title);
  169. }
  170.  
  171. public function writeFile($fullpath)
  172. {
  173. // Delete the template sheet
  174. $this->spreadsheet->deleteSheet($this->templateSheet);
  175. $this->spreadsheet->save($fullpath);
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement