| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- namespace App\Exports;
- use Maatwebsite\Excel\Concerns\WithEvents;
- use Maatwebsite\Excel\Concerns\WithTitle;
- use Maatwebsite\Excel\Events\AfterSheet;
- use PhpOffice\PhpSpreadsheet\Style\Alignment;
- use PhpOffice\PhpSpreadsheet\Style\Border;
- class CompanyRdActivityExport implements WithEvents, WithTitle
- {
- protected $data; // 存放传入的列表数据
- public function __construct(array $data) {
- $this->data = $data;
- }
- public function title(): string {
- return '研发活动汇总';
- }
- public function registerEvents(): array {
- return [
- AfterSheet::class => function (AfterSheet $event) {
- $sheet = $event->sheet->getDelegate();
- $lastCol = 'H';
- $dataCount = count($this->data);
- $lastRow = 3 + $dataCount; // 数据从第4行开始填充
- // --- 1. 第一行:主标题 ---
- $sheet->mergeCells('A1:H1');
- $sheet->setCellValue('A1', "企业研究开发活动汇总表");
- // --- 2. 第二行:单位说明 ---
- $sheet->mergeCells('A2:H2');
- $sheet->setCellValue('A2', "单位:元 "); // 末尾加空格防止贴边
- // --- 3. 第三行:表头标题 ---
- $headers = [
- 'A'=>'序号','B'=>'研发活动名称','C'=>'起止时间',
- 'D'=>'技术来源','E'=>'研发预算','F'=>'实际支出',
- 'G'=>'技术领域','H'=>'备注'
- ];
- foreach ($headers as $col => $title) {
- $sheet->setCellValue("{$col}3", $title);
- }
- // --- 4. 填充数据 (从第4行开始) ---
- $currentRow = 4;
- foreach ($this->data as $index => $row) {
- $sheet->fromArray([
- $index + 1, // A 序号
- $row['activity_name']??'', // B 研发活动名称
- $row['time_range']??'', // C 起止时间
- $row['from'], // D 技术来源
- $row['budget']??0, // E 研发预算
- $row['actual_spending']??0, // F 实际支出
- $row['tech_area']??'', // G 技术领域
- $row['remark']??'', // H 备注
- ], null, "A{$currentRow}");
- $currentRow++;
- }
- // --- 5. 样式全局精修 ---
- $fullRange = "A1:{$lastCol}{$lastRow}";
- // 统一设置:垂直居中、水平居中、自动换行
- $sheet->getStyle($fullRange)->getAlignment()->applyFromArray([
- 'vertical' => Alignment::VERTICAL_CENTER,
- 'horizontal' => Alignment::HORIZONTAL_CENTER,
- 'wrapText' => true,
- ]);
- // --- 【关键修正】第二行:单位说明 强制右对齐 ---
- // 必须在全局居中设置之后执行,否则会被覆盖
- $sheet->getStyle('A2:H2')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);
- // 表头 (第1行和第3行) 字体加粗
- $sheet->getStyle('A1:H1')->getFont()->setBold(true)->setSize(14);
- $sheet->getStyle('A3:H3')->getFont()->setBold(true);
- // --- 6. 调整行高与列宽 ---
- $sheet->getRowDimension(1)->setRowHeight(35); // 主标题行
- $sheet->getRowDimension(2)->setRowHeight(20); // 单位行
- $sheet->getRowDimension(3)->setRowHeight(25); // 表头行
- // 设置列宽
- $sheet->getColumnDimension('A')->setWidth(8);
- $sheet->getColumnDimension('B')->setWidth(30);
- $sheet->getColumnDimension('C')->setWidth(18);
- $sheet->getColumnDimension('D')->setWidth(15);
- $sheet->getColumnDimension('E')->setWidth(13);
- $sheet->getColumnDimension('F')->setWidth(13);
- $sheet->getColumnDimension('G')->setWidth(15);
- $sheet->getColumnDimension('H')->setWidth(25);
- // --- 7. 全表加细边框 ---
- $sheet->getStyle("A3:{$lastCol}{$lastRow}")->getBorders()->getAllBorders()->setBorderStyle(Border::BORDER_THIN);
- },
- ];
- }
- }
|