ExportOrder.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace App\Exports;
  3. use Illuminate\Support\Collection;
  4. use Illuminate\Support\Facades\Redis;
  5. use Maatwebsite\Excel\Concerns\FromCollection;
  6. use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
  7. use Maatwebsite\Excel\Concerns\WithEvents; // 自动注册事件监听器
  8. use Maatwebsite\Excel\Concerns\WithHeadings;
  9. use Maatwebsite\Excel\Concerns\WithStrictNullComparison; // 导出 0 原样显示,不为 null
  10. use Maatwebsite\Excel\Events\AfterSheet;
  11. use PhpOffice\PhpSpreadsheet\Cell\Cell;
  12. use PhpOffice\PhpSpreadsheet\Cell\DataType;
  13. use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
  14. class ExportOrder extends DefaultValueBinder implements WithCustomValueBinder , FromCollection, WithEvents, WithStrictNullComparison,withHeadings
  15. {
  16. /**
  17. * @return \Illuminate\Support\Collection
  18. */
  19. public function __construct($data,$type=1,$headers)
  20. {
  21. $this->data = $data;
  22. $this->type = $type;
  23. $this->headers = $headers;
  24. }
  25. public function registerEvents(): array
  26. {
  27. //区分不通状态的合同导出,格式不同
  28. $type = $this->type.'_set';
  29. return $this->$type();
  30. }
  31. //数组转集合
  32. public function collection()
  33. {
  34. return new Collection($this->createData());
  35. }
  36. //业务代码
  37. public function createData()
  38. {
  39. $name = $this->type;
  40. $data = $this->export();
  41. return $data;
  42. }
  43. public function bindValue(Cell $cell, $value)
  44. {
  45. if (is_numeric($value)) {
  46. $cell->setValueExplicit($value, DataType::TYPE_STRING2);
  47. return true;
  48. }
  49. // else return default behavior
  50. return parent::bindValue($cell, $value);
  51. }
  52. // 自定义表头,需实现withHeadings接口
  53. public function headings(): array
  54. {
  55. return $this->headers;
  56. }
  57. private function export(){
  58. $list = [];
  59. // dump($this->data);die;
  60. foreach ($this->data as $v){
  61. $list[] = $v;
  62. }
  63. return $list;
  64. }
  65. private function default_set(){
  66. return [
  67. AfterSheet::class => function (AfterSheet $event) {
  68. $count = count($this->data);
  69. //设置区域单元格水平居中
  70. $event->sheet->getDelegate()->getStyle('A1:'.'M'.($count+1))->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
  71. // 定义列宽度
  72. $widths = ['A' => 20, 'B' => 20, 'C' => 20, 'D' => 20, 'E' => 20, 'F' => 20, 'G' => 20, 'H' => 20, 'I' => 20, 'J' => 20, 'K' => 20, 'L' => 20, 'M' => 20, 'O' => 20, 'P' => 20, 'Q' => 20, 'R' => 20, 'S' => 20, 'T' => 20, 'U' => 20, 'V' => 20, 'W' => 20, 'X' => 20, 'Y' => 20, 'Z' => 20];
  73. foreach ($widths as $k => $v) {
  74. // 设置列宽度
  75. $event->sheet->getDelegate()->getColumnDimension($k)->setWidth($v);
  76. }
  77. },
  78. ];
  79. }
  80. private function kq_set(){
  81. return [
  82. AfterSheet::class => function(AfterSheet $event) {
  83. $event->sheet->getStyle('A1:D1')->applyFromArray([
  84. 'font' => ['bold' => true],
  85. 'fill' => ['fillType' => 'solid', 'color' => ['rgb' => 'FFCC00']],
  86. 'borders' => ['allBorders' => ['borderStyle' => 'thin']]
  87. ]);
  88. // 自动调整列宽
  89. for ($i = 'A'; $i !== 'E'; $i++) {
  90. $event->sheet->getColumnDimension($i)->setAutoSize(true);
  91. }
  92. },
  93. ];
  94. }
  95. private function kq2_set(){
  96. return [
  97. AfterSheet::class => function(AfterSheet $event) {
  98. // 自动调整列宽
  99. for ($i = 'A'; $i !== 'E'; $i++) {
  100. $event->sheet->getColumnDimension($i)->setAutoSize(true);
  101. }
  102. // 获取工作表实例
  103. $sheet = $event->sheet->getDelegate();
  104. // 合并第一行单元格(A1到E1)
  105. $sheet->mergeCells('A1:AD1');
  106. $sheet->mergeCells('A2:B2');
  107. $sheet->mergeCells('A3:B3');
  108. // 设置第一行样式
  109. $styleArray = [
  110. 'font' => [
  111. 'bold' => true, // 加粗
  112. ],
  113. 'alignment' => [
  114. 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER, // 水平居中
  115. 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER, // 垂直居中
  116. ],
  117. ];
  118. // 应用样式到第一行
  119. $sheet->getStyle('A1:E1')->applyFromArray($styleArray);
  120. $sheet->getStyle('A2:B2')->applyFromArray($styleArray);
  121. $sheet->getStyle('A3:B3')->applyFromArray($styleArray);
  122. $lastRow = $sheet->getHighestRow();
  123. $range = 'A' . $lastRow . ':B' . $lastRow;
  124. $sheet->mergeCells($range);
  125. $sheet->getStyle($range)->applyFromArray($styleArray);
  126. },
  127. ];
  128. }
  129. }