MacroExcel.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. declare(strict_types=1);
  3. namespace catcher\library\excel;
  4. trait MacroExcel
  5. {
  6. /**
  7. * @var string
  8. */
  9. protected $start = 'A';
  10. /**
  11. * 开始行
  12. *
  13. * @var int
  14. */
  15. protected $row = 1;
  16. /**
  17. * @var array
  18. */
  19. protected $columns = [];
  20. /**
  21. * 设置开始的单元
  22. *
  23. * @time 2020年05月25日
  24. * @return string
  25. */
  26. protected function getStartSheet(): string
  27. {
  28. if (method_exists($this->excel, 'start')) {
  29. $this->start = $this->excel->start();
  30. }
  31. return $this->start;
  32. }
  33. /**
  34. * 设置单元格宽度
  35. *
  36. * @time 2020年05月25日
  37. * @return void
  38. */
  39. protected function setSheetWidth()
  40. {
  41. if (method_exists($this->excel, 'setWidth')) {
  42. $width = $this->excel->setWidth();
  43. foreach ($width as $sheet => $w) {
  44. $this->getWorksheet()->getColumnDimension($sheet)->setWidth($w);
  45. }
  46. }
  47. }
  48. /**
  49. * before
  50. *
  51. * @time 2020年05月25日
  52. * @return void
  53. */
  54. protected function before()
  55. {
  56. if (method_exists($this->excel, 'before')) {
  57. $this->excel->before();
  58. }
  59. }
  60. /**
  61. * 设置 column 信息 ['A', 'B', 'C' ...]
  62. *
  63. * @time 2020年05月25日
  64. * @return array
  65. */
  66. protected function getSheetColumns()
  67. {
  68. if (empty($this->columns)) {
  69. $start = $this->getStartSheet();
  70. $columns = [];
  71. // 通过 headers 推断需要的 columns
  72. foreach ($this->excel->headers() as $k => $header) {
  73. $columns[] = chr(ord($start) + $k);
  74. }
  75. return $columns;
  76. }
  77. return $this->columns;
  78. }
  79. /**
  80. * set keys
  81. *
  82. * @time 2020年05月25日
  83. * @return array
  84. */
  85. protected function getKeys()
  86. {
  87. if (method_exists($this->excel, 'keys')) {
  88. return $this->excel->keys();
  89. }
  90. return [];
  91. }
  92. /**
  93. * set start row
  94. *
  95. * @time 2020年05月25日
  96. * @return int
  97. */
  98. protected function getStartRow()
  99. {
  100. if (method_exists($this->excel, 'setRow')) {
  101. $this->row = $this->excel->setRow();
  102. }
  103. return $this->row;
  104. }
  105. /**
  106. * 设置 title
  107. *
  108. * @time 2020年05月25日
  109. * @return void
  110. */
  111. protected function setTitle()
  112. {
  113. if (method_exists($this->excel, 'setTitle')) {
  114. [$cells, $title, $style] = $this->excel->setTitle();
  115. $this->getWorksheet()
  116. ->mergeCells($cells) // 合并单元格
  117. ->setCellValue(explode(':', $cells)[0], $title)
  118. ->getStyle($cells) // 设置样式
  119. ->getAlignment()
  120. ->setHorizontal($style);
  121. }
  122. }
  123. /**
  124. * register worksheet for excel
  125. *
  126. * @time 2020年05月25日
  127. * @return void
  128. */
  129. protected function registerWorksheet()
  130. {
  131. if (method_exists($this->excel, 'getWorksheet')) {
  132. $this->excel->getWorksheet($this->getWorksheet());
  133. }
  134. }
  135. /**
  136. * 增加 start row
  137. *
  138. * @time 2020年05月25日
  139. * @return void
  140. */
  141. protected function incRow()
  142. {
  143. ++$this->row;
  144. }
  145. /**
  146. * 设置内存限制
  147. *
  148. * @time 2020年05月26日
  149. * @return void
  150. */
  151. public function setMemoryLimit()
  152. {
  153. if (property_exists($this->excel, 'memory')) {
  154. ini_set('memory_limit', $this->excel->memory);
  155. }
  156. }
  157. }