data = $data; $this->headers = $headers; $this->headerComments = $headerComments; } public function collection() { return new Collection($this->createData()); } public function createData() { return $this->export(); } public function headings(): array { return $this->headers; } private function export() { $list = []; foreach ($this->data as $v) { $list[] = $v; } return $list; } public function registerEvents(): array { return [ AfterSheet::class => function(AfterSheet $event) { if (empty($this->headerComments)) { return; // 不传就不加批注 } $sheet = $event->sheet->getDelegate(); foreach ($this->headers as $index => $headerName) { if (!isset($this->headerComments[$headerName])) { continue; // 该表头没设置批注 } $columnLetter = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($index + 1); $cell = $columnLetter . '1'; $commentText = $this->headerComments[$headerName]; // 创建批注 $comment = $sheet->getComment($cell); $comment->getText()->createTextRun($commentText); $comment->setAuthor('系统提示'); // 设置批注框大小(可调整数值) $comment->setWidth('200pt'); // 宽度 $comment->setHeight('100pt'); // 高度 } } ]; } }