Export.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. namespace App\Exports;
  3. use Illuminate\Support\Collection;
  4. use Maatwebsite\Excel\Concerns\FromCollection;
  5. use Maatwebsite\Excel\Concerns\WithEvents;
  6. use Maatwebsite\Excel\Concerns\WithHeadings;
  7. use Maatwebsite\Excel\Events\AfterSheet;
  8. use PhpOffice\PhpSpreadsheet\Style\Border;
  9. /**
  10. * 导出Excel类
  11. * @author 牧羊人
  12. * @since 2021/4/10
  13. * Class Export
  14. * @package App\Exports
  15. */
  16. class Export implements FromCollection, WithHeadings, WithEvents
  17. {
  18. protected $data;
  19. protected $headings;
  20. protected $columnWidth = [];//设置列宽 key:列 value:宽
  21. protected $rowHeight = []; //设置行高 key:行 value:高
  22. protected $mergeCells = []; //合并单元格 value:A1:K8
  23. protected $font = []; //设置字体 key:A1:K8 value:Arial
  24. protected $fontSize = []; //设置字体大小 key:A1:K8 value:11
  25. protected $bold = []; //设置粗体 key:A1:K8 value:true
  26. protected $background = []; //设置背景颜色 key:A1:K8 value:#F0F0F0F
  27. protected $vertical = []; //设置定位 key:A1:K8 value:center
  28. protected $sheetName; //sheet title
  29. protected $borders = []; //设置边框颜色 key:A1:K8 value:#000000
  30. //设置页面属性时如果无效 更改excel格式尝试即可
  31. //构造函数传值
  32. public function __construct($data, $headings, $sheetName)
  33. {
  34. $this->data = $data;
  35. $this->headings = $headings;
  36. $this->sheetName = $sheetName;
  37. $this->createData();
  38. }
  39. public function headings(): array
  40. {
  41. return $this->headings;
  42. }
  43. //数组转集合
  44. public function collection()
  45. {
  46. return new Collection($this->data);
  47. }
  48. //业务代码
  49. public function createData()
  50. {
  51. $this->data = collect($this->data)->toArray();
  52. }
  53. public function registerEvents(): array
  54. {
  55. return [
  56. AfterSheet::class => function (AfterSheet $event) {
  57. //设置区域单元格垂直居中
  58. $event->sheet->getDelegate()->getStyle('A1:Z1265')->getAlignment()->setVertical('center');
  59. //设置区域单元格水平居中
  60. $event->sheet->getDelegate()->getStyle('A1:Z1265')->getAlignment()->setHorizontal('center');
  61. //设置列宽
  62. foreach ($this->columnWidth as $column => $width) {
  63. $event->sheet->getDelegate()
  64. ->getColumnDimension($column)
  65. ->setWidth($width);
  66. }
  67. //设置行高,$i为数据行数
  68. foreach ($this->rowHeight as $row => $height) {
  69. $event->sheet->getDelegate()
  70. ->getRowDimension($row)
  71. ->setRowHeight($height);
  72. }
  73. //设置区域单元格垂直居中
  74. foreach ($this->vertical as $region => $position) {
  75. $event->sheet->getDelegate()
  76. ->getStyle($region)
  77. ->getAlignment()
  78. ->setVertical($position);
  79. }
  80. //设置区域单元格字体
  81. foreach ($this->font as $region => $value) {
  82. $event->sheet->getDelegate()
  83. ->getStyle($region)
  84. ->getFont()->setName($value);
  85. }
  86. //设置区域单元格字体大小
  87. foreach ($this->fontSize as $region => $value) {
  88. $event->sheet->getDelegate()
  89. ->getStyle($region)
  90. ->getFont()
  91. ->setSize($value);
  92. }
  93. //设置区域单元格字体粗体
  94. foreach ($this->bold as $region => $bool) {
  95. $event->sheet->getDelegate()
  96. ->getStyle($region)
  97. ->getFont()
  98. ->setBold($bool);
  99. }
  100. //设置区域单元格背景颜色
  101. foreach ($this->background as $region => $item) {
  102. $event->sheet->getDelegate()->getStyle($region)->applyFromArray([
  103. 'fill' => [
  104. 'fillType' => 'linear', //线性填充,类似渐变
  105. 'startColor' => [
  106. 'rgb' => $item //初始颜色
  107. ],
  108. //结束颜色,如果需要单一背景色,请和初始颜色保持一致
  109. 'endColor' => [
  110. 'argb' => $item
  111. ]
  112. ]
  113. ]);
  114. }
  115. //设置边框颜色
  116. foreach ($this->borders as $region => $item) {
  117. $event->sheet->getDelegate()->getStyle($region)->applyFromArray([
  118. 'borders' => [
  119. 'allBorders' => [
  120. 'borderStyle' => Border::BORDER_THIN,
  121. 'color' => ['argb' => $item],
  122. ],
  123. ],
  124. ]);
  125. }
  126. //合并单元格
  127. $event->sheet->getDelegate()->setMergeCells($this->mergeCells);
  128. if (!empty($this->sheetName)) {
  129. $event->sheet->getDelegate()->setTitle($this->sheetName);
  130. }
  131. }
  132. ];
  133. }
  134. /**
  135. * @return array
  136. * [
  137. * 'B' => 40,
  138. * 'C' => 60
  139. * ]
  140. */
  141. public function setColumnWidth(array $columnwidth)
  142. {
  143. $this->columnWidth = array_change_key_case($columnwidth, CASE_UPPER);
  144. }
  145. /**
  146. * @return array
  147. * [
  148. * 1 => 40,
  149. * 2 => 60
  150. * ]
  151. */
  152. public function setRowHeight(array $rowHeight)
  153. {
  154. $this->rowHeight = $rowHeight;
  155. }
  156. /**
  157. * @return array
  158. * [
  159. * A1:K7 => '宋体'
  160. * ]
  161. */
  162. public function setFont(array $font)
  163. {
  164. $this->font = array_change_key_case($font, CASE_UPPER);
  165. }
  166. /**
  167. * @return array
  168. * @2020/3/22 10:33
  169. * [
  170. * A1:K7 => true
  171. * ]
  172. */
  173. public function setBold(array $bold)
  174. {
  175. $this->bold = array_change_key_case($bold, CASE_UPPER);
  176. }
  177. /**
  178. * @return array
  179. * @2020/3/22 10:33
  180. * [
  181. * A1:K7 => F0FF0F
  182. * ]
  183. */
  184. public function setBackground(array $background)
  185. {
  186. $this->background = array_change_key_case($background, CASE_UPPER);
  187. }
  188. /**
  189. * @return array
  190. * [
  191. * A1:K7
  192. * ]
  193. */
  194. public function setMergeCells(array $mergeCells)
  195. {
  196. $this->mergeCells = array_change_key_case($mergeCells, CASE_UPPER);
  197. }
  198. /**
  199. * @return array
  200. * [
  201. * A1:K7 => 14
  202. * ]
  203. */
  204. public function setFontSize(array $fontSize)
  205. {
  206. $this->fontSize = array_change_key_case($fontSize, CASE_UPPER);
  207. }
  208. /**
  209. * @return array
  210. * [
  211. * A1:K7 => #000000
  212. * ]
  213. */
  214. public function setBorders(array $borders)
  215. {
  216. $this->borders = array_change_key_case($borders, CASE_UPPER);
  217. }
  218. }