ExportOrder.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Exports;
  3. use Illuminate\Support\Collection;
  4. use Maatwebsite\Excel\Concerns\FromCollection;
  5. use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
  6. use Maatwebsite\Excel\Concerns\WithEvents; // 自动注册事件监听器
  7. use Maatwebsite\Excel\Concerns\WithHeadings;
  8. use Maatwebsite\Excel\Concerns\WithStrictNullComparison; // 导出 0 原样显示,不为 null
  9. use Maatwebsite\Excel\Events\AfterSheet;
  10. use PhpOffice\PhpSpreadsheet\Cell\Cell;
  11. use PhpOffice\PhpSpreadsheet\Cell\DataType;
  12. use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
  13. class ExportOrder extends DefaultValueBinder implements WithCustomValueBinder , FromCollection, WithEvents, WithStrictNullComparison,withHeadings
  14. {
  15. /**
  16. * @return \Illuminate\Support\Collection
  17. */
  18. public function __construct($data,$type=1,$headers)
  19. {
  20. $this->data = $data;
  21. $this->type = $type;
  22. $this->headers = $headers;
  23. }
  24. public function registerEvents(): array
  25. {
  26. //区分不通状态的合同导出,格式不同
  27. $type = $this->type.'_set';
  28. return $this->$type();
  29. }
  30. //数组转集合
  31. public function collection()
  32. {
  33. return new Collection($this->createData());
  34. }
  35. //业务代码
  36. public function createData()
  37. {
  38. $name = $this->type;
  39. $data = $this->export();
  40. return $data;
  41. }
  42. public function bindValue(Cell $cell, $value)
  43. {
  44. // 检查是否需要保留前导零
  45. // if (is_string($value) && ctype_digit($value) && strpos($value, '0') === 0) {
  46. // $cell->setValueExplicit($value, DataType::TYPE_STRING);
  47. // return true;
  48. // }
  49. if (is_numeric($value)) {
  50. $cell->setValueExplicit($value, DataType::TYPE_STRING2);
  51. return true;
  52. }
  53. // else return default behavior
  54. return parent::bindValue($cell, $value);
  55. }
  56. // 自定义表头,需实现withHeadings接口
  57. public function headings(): array
  58. {
  59. return $this->headers;
  60. }
  61. private function export(){
  62. $list = [];
  63. foreach ($this->data as $v){
  64. $list[] = $v;
  65. }
  66. return $list;
  67. }
  68. private function default_set(){
  69. return [
  70. AfterSheet::class => function (AfterSheet $event) {
  71. $count = count($this->data);
  72. //设置区域单元格水平居中
  73. $event->sheet->getDelegate()->getStyle('A1:'.'M'.($count+1))->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
  74. // 定义列宽度
  75. $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];
  76. foreach ($widths as $k => $v) {
  77. // 设置列宽度
  78. $event->sheet->getDelegate()->getColumnDimension($k)->setWidth($v);
  79. }
  80. },
  81. ];
  82. }
  83. private function salary_set(){
  84. return [
  85. AfterSheet::class => function (AfterSheet $event) {
  86. $count = count($this->data);
  87. //设置区域单元格水平居中
  88. $event->sheet->getDelegate()->getStyle('A1:'.'M'.($count+2))->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
  89. // 定义列宽度
  90. $widths = ['A' => 20, 'B' => 20, 'C' => 20, 'D' => 20, 'E' => 20, 'F' => 35, 'G' => 25, 'H' => 25, 'I' => 25, 'J' => 25, 'K' => 25, 'L' => 25, 'M' => 25];
  91. foreach ($widths as $k => $v) {
  92. // 设置列宽度
  93. $event->sheet->getDelegate()->getColumnDimension($k)->setWidth($v);
  94. }
  95. },
  96. ];
  97. }
  98. private function jc_set(){
  99. return [
  100. AfterSheet::class => function (AfterSheet $event) {
  101. $count = count($this->data);
  102. // 合并表头单元格
  103. $event->sheet->getDelegate()->mergeCells('A1:A2');
  104. $event->sheet->getDelegate()->mergeCells('B1:B2');
  105. $event->sheet->getDelegate()->mergeCells('C1:E1');
  106. $event->sheet->getDelegate()->mergeCells('F1:H1');
  107. $event->sheet->getDelegate()->mergeCells('I1:K1');
  108. $event->sheet->getDelegate()->mergeCells('L1:N1');
  109. //设置区域单元格水平居中
  110. $event->sheet->getDelegate()->getStyle('A1:'.'M'.($count+2))->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
  111. // 定义列宽度
  112. $widths = ['A' => 20, 'B' => 20, 'C' => 10, 'D' => 10, 'E' => 10, 'F' => 10, 'G' => 10, 'H' => 10, 'I' => 10, 'J' => 10, 'K' => 10, 'L' => 10, 'M' => 10];
  113. foreach ($widths as $k => $v) {
  114. // 设置列宽度
  115. $event->sheet->getDelegate()->getColumnDimension($k)->setWidth($v);
  116. }
  117. },
  118. ];
  119. }
  120. }