Exports.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 Exports 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. //use Maatwebsite\Excel\Concerns\WithColumnFormatting;
  53. //use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
  54. // public function columnFormats(): array
  55. // {
  56. // return [
  57. // 'F' => NumberFormat::FORMAT_NUMBER,
  58. // ];
  59. // }
  60. // 自定义表头,需实现withHeadings接口
  61. public function headings(): array
  62. {
  63. return $this->headers;
  64. }
  65. private function export(){
  66. $list = [];
  67. foreach ($this->data as $v){
  68. $list[] = $v;
  69. }
  70. return $list;
  71. }
  72. private function production_order_set(){
  73. return [
  74. AfterSheet::class => function (AfterSheet $event) {
  75. $count = count($this->data);
  76. //设置区域单元格水平居中
  77. $event->sheet->getDelegate()->getStyle('A1:'.'M'.($count+1))->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
  78. // 定义列宽度
  79. $widths = ['A' => 20, 'B' => 20, 'C' => 20, 'D' => 20, 'E' => 20, 'F' => 35, 'G' => 25, 'H' => 25, 'I' => 25, 'J' => 25];
  80. foreach ($widths as $k => $v) {
  81. // 设置列宽度
  82. $event->sheet->getDelegate()->getColumnDimension($k)->setWidth($v);
  83. }
  84. $row = 2;
  85. //设置字段
  86. foreach ($this->data as $item) {
  87. $event->sheet->setCellValue('A'.$row, $item['production_time']);
  88. $event->sheet->setCellValue('B'.$row, $item['production_no']);
  89. $event->sheet->setCellValue('C'.$row, $item['out_order_no_time']);
  90. $event->sheet->setCellValue('D'.$row, $item['out_order_no']);
  91. $event->sheet->setCellValue('E'.$row, $item['customer_no']);
  92. $event->sheet->setCellValue('F'.$row, $item['customer_name']);
  93. $event->sheet->setCellValue('G'.$row, $item['table_header_mark']);
  94. $event->sheet->setCellValue('H'.$row, $item['product_no']);
  95. $event->sheet->setCellValue('I'.$row, $item['product_title']);
  96. $event->sheet->setCellValue('J'.$row, $item['product_size']);
  97. $event->sheet->setCellValue('K'.$row, $item['product_unit']);
  98. $event->sheet->setCellValue('L'.$row, $item['order_quantity']);
  99. $event->sheet->setCellValue('M'.$row, $item['production_quantity']);
  100. $event->sheet->setCellValue('N'.$row, $item['not_production']);
  101. $event->sheet->setCellValue('O'.$row, $item['technology_material']);
  102. $event->sheet->setCellValue('P'.$row, $item['technology_name']);
  103. $event->sheet->setCellValue('Q'.$row, $item['wood_name']);
  104. $event->sheet->setCellValue('R'.$row, $item['process_mark']);
  105. $event->sheet->setCellValue('S'.$row, $item['table_body_mark']);
  106. $event->sheet->setCellValue('T'.$row, $item['out_crt_man']);
  107. $row++; // 行数增加
  108. }
  109. },
  110. ];
  111. }
  112. }