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_numeric($value)) { $cell->setValueExplicit($value, DataType::TYPE_STRING2); return true; } // else return default behavior return parent::bindValue($cell, $value); } //use Maatwebsite\Excel\Concerns\WithColumnFormatting; //use PhpOffice\PhpSpreadsheet\Style\NumberFormat; // public function columnFormats(): array // { // return [ // 'F' => NumberFormat::FORMAT_NUMBER, // ]; // } // 自定义表头,需实现withHeadings接口 public function headings(): array { return $this->headers; } private function export(){ $list = []; foreach ($this->data as $v){ $list[] = $v; } return $list; } private function production_order_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' => 35, 'G' => 25, 'H' => 25, 'I' => 25, 'J' => 25]; foreach ($widths as $k => $v) { // 设置列宽度 $event->sheet->getDelegate()->getColumnDimension($k)->setWidth($v); } $row = 2; //设置字段 foreach ($this->data as $item) { $event->sheet->setCellValue('A'.$row, $item['production_time']); $event->sheet->setCellValue('B'.$row, $item['production_no']); $event->sheet->setCellValue('C'.$row, $item['out_order_no_time']); $event->sheet->setCellValue('D'.$row, $item['out_order_no']); $event->sheet->setCellValue('E'.$row, $item['customer_no']); $event->sheet->setCellValue('F'.$row, $item['customer_name']); $event->sheet->setCellValue('G'.$row, $item['table_header_mark']); $event->sheet->setCellValue('H'.$row, $item['product_no']); $event->sheet->setCellValue('I'.$row, $item['product_title']); $event->sheet->setCellValue('J'.$row, $item['product_size']); $event->sheet->setCellValue('K'.$row, $item['product_unit']); $event->sheet->setCellValue('L'.$row, $item['order_quantity']); $event->sheet->setCellValue('M'.$row, $item['production_quantity']); $event->sheet->setCellValue('N'.$row, $item['not_production']); $event->sheet->setCellValue('O'.$row, $item['technology_material']); $event->sheet->setCellValue('P'.$row, $item['technology_name']); $event->sheet->setCellValue('Q'.$row, $item['wood_name']); $event->sheet->setCellValue('R'.$row, $item['process_mark']); $event->sheet->setCellValue('S'.$row, $item['table_body_mark']); $event->sheet->setCellValue('T'.$row, $item['out_crt_man']); $row++; // 行数增加 } }, ]; } }