data = $data; $this->fields = $fields; $this->sheetName = $sheetName; } public function collection() { $newList = []; // 添加表头 $header = array_values($this->fields); $newList[] = $header; // 添加数据行 foreach ($this->data as $row) { $temp = []; foreach (array_keys($this->fields) as $field) { $temp[] = $row[$field] ?? ''; } $newList[] = $temp; } return new Collection($newList); } public function styles(Worksheet $sheet) { return [ 1 => [ 'font' => ['bold' => true], 'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER], ], ]; } public function columnWidths(): array { $widths = []; $column = 'A'; foreach ($this->fields as $field) { $widths[$column] = 15; $column++; } return $widths; } public function registerEvents(): array { return [ AfterSheet::class => function (AfterSheet $event) { $sheet = $event->sheet->getDelegate(); $sheet->setTitle($this->sheetName); }, ]; } }