cqp hace 3 meses
padre
commit
d656dfffa6
Se han modificado 1 ficheros con 143 adiciones y 0 borrados
  1. 143 0
      app/Exports/ExportOrder.php

+ 143 - 0
app/Exports/ExportOrder.php

@@ -0,0 +1,143 @@
+<?php
+
+namespace App\Exports;
+
+use Illuminate\Support\Collection;
+use Maatwebsite\Excel\Concerns\FromCollection;
+use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
+use Maatwebsite\Excel\Concerns\WithEvents;     // 自动注册事件监听器
+use Maatwebsite\Excel\Concerns\WithHeadings;
+use Maatwebsite\Excel\Concerns\WithStrictNullComparison;    // 导出 0 原样显示,不为 null
+use Maatwebsite\Excel\Events\AfterSheet;
+use PhpOffice\PhpSpreadsheet\Cell\Cell;
+use PhpOffice\PhpSpreadsheet\Cell\DataType;
+use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
+
+class ExportOrder extends DefaultValueBinder implements WithCustomValueBinder , FromCollection, WithEvents, WithStrictNullComparison,withHeadings
+{
+    /**
+     * @return \Illuminate\Support\Collection
+     */
+    public function __construct($data,$type=1,$headers)
+    {
+        $this->data = $data;
+        $this->type = $type;
+        $this->headers = $headers;
+    }
+
+    public function registerEvents(): array
+    {
+        //区分不通状态的合同导出,格式不同
+            $type = $this->type.'_set';
+            return $this->$type();
+    }
+
+    //数组转集合
+    public function collection()
+    {
+        return new Collection($this->createData());
+    }
+
+    //业务代码
+    public function createData()
+    {
+        $name = $this->type;
+        $data = $this->export();
+        return $data;
+
+    }
+
+    public function bindValue(Cell $cell, $value)
+    {
+        // 检查是否需要保留前导零
+//        if (is_string($value) && ctype_digit($value) && strpos($value, '0') === 0) {
+//            $cell->setValueExplicit($value, DataType::TYPE_STRING);
+//            return true;
+//        }
+
+        if (is_numeric($value)) {
+            $cell->setValueExplicit($value, DataType::TYPE_STRING2);
+
+            return true;
+        }
+
+        // else return default behavior
+        return parent::bindValue($cell, $value);
+    }
+
+    // 自定义表头,需实现withHeadings接口
+    public function headings(): array
+    {
+        return $this->headers;
+    }
+
+    private function export(){
+        $list = [];
+        foreach ($this->data as $v){
+            $list[] = $v;
+        }
+        return $list;
+    }
+
+    private function default_set(){
+        return [
+            AfterSheet::class => function (AfterSheet $event) {
+                $count = count($this->data);
+
+                //设置区域单元格水平居中
+                $event->sheet->getDelegate()->getStyle('A1:'.'M'.($count+1))->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
+
+                // 定义列宽度
+                $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];
+                foreach ($widths as $k => $v) {
+                    // 设置列宽度
+                    $event->sheet->getDelegate()->getColumnDimension($k)->setWidth($v);
+                }
+            },
+        ];
+    }
+
+    private function salary_set(){
+        return [
+            AfterSheet::class => function (AfterSheet $event) {
+                $count = count($this->data);
+
+                //设置区域单元格水平居中
+                $event->sheet->getDelegate()->getStyle('A1:'.'M'.($count+2))->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
+
+                // 定义列宽度
+                $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];
+                foreach ($widths as $k => $v) {
+                    // 设置列宽度
+                    $event->sheet->getDelegate()->getColumnDimension($k)->setWidth($v);
+                }
+            },
+        ];
+    }
+
+    private function jc_set(){
+        return [
+            AfterSheet::class => function (AfterSheet $event) {
+                $count = count($this->data);
+
+                // 合并表头单元格
+                $event->sheet->getDelegate()->mergeCells('A1:A2');
+                $event->sheet->getDelegate()->mergeCells('B1:B2');
+                $event->sheet->getDelegate()->mergeCells('C1:E1');
+                $event->sheet->getDelegate()->mergeCells('F1:H1');
+                $event->sheet->getDelegate()->mergeCells('I1:K1');
+                $event->sheet->getDelegate()->mergeCells('L1:N1');
+
+                //设置区域单元格水平居中
+                $event->sheet->getDelegate()->getStyle('A1:'.'M'.($count+2))->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
+
+                // 定义列宽度
+                $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];
+                foreach ($widths as $k => $v) {
+                    // 设置列宽度
+                    $event->sheet->getDelegate()->getColumnDimension($k)->setWidth($v);
+                }
+            },
+        ];
+    }
+}