| 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;
- use PhpOffice\PhpSpreadsheet\Cell\DataType;
- class TechnicalStaffExport 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 = 2 + $dataCount;
- // --- 1. 第一行:主标题 ---
- $sheet->mergeCells('A1:H1');
- $sheet->setCellValue('A1', "企业研究开发科技人员情况表");
- $sheet->getStyle('A1')->getFont()->setBold(true)->setSize(16);
- // --- 2. 第二行:表头 ---
- $headers = [
- 'A' => '序号',
- 'B' => '姓名',
- 'C' => '身份证号码',
- 'D' => '学历',
- 'E' => '专业',
- 'F' => '职称/职业资格',
- 'G' => '部门/岗位',
- 'H' => '聘用类型',
- ];
- foreach ($headers as $col => $title) {
- $sheet->setCellValue("{$col}2", $title);
- }
- $sheet->getStyle('A2:H2')->getFont()->setBold(true);
- // --- 3. 填充数据 (解决科学计数法关键) ---
- $currentRow = 3;
- foreach ($this->data as $index => $row) {
- // 序号、姓名正常填充
- $sheet->setCellValue("A{$currentRow}", $index + 1);
- $sheet->setCellValue("B{$currentRow}", $row['name'] ?? '');
- // 【关键点】显式设置身份证列为文本格式 (Explicitly set as String)
- $idCard = $row['id_card'] ?? '';
- $sheet->setCellValueExplicit("C{$currentRow}", $idCard, DataType::TYPE_STRING);
- // 其他字段
- $sheet->setCellValue("D{$currentRow}", $row['education'] ?? '');
- $sheet->setCellValue("E{$currentRow}", $row['major'] ?? '');
- $sheet->setCellValue("F{$currentRow}", $row['title_level'] ?? '');
- $sheet->setCellValue("G{$currentRow}", $row['department_job'] ?? '');
- $sheet->setCellValue("H{$currentRow}", $row['employment_type'] ?? '');
- $currentRow++;
- }
- // --- 4. 样式全局精修 ---
- $fullRange = "A1:{$lastCol}{$lastRow}";
- $sheet->getStyle($fullRange)->getAlignment()->applyFromArray([
- 'vertical' => Alignment::VERTICAL_CENTER,
- 'horizontal' => Alignment::HORIZONTAL_CENTER,
- 'wrapText' => true,
- ]);
- // --- 5. 设置行高与列宽 ---
- $sheet->getRowDimension(1)->setRowHeight(40);
- $sheet->getRowDimension(2)->setRowHeight(30);
- $sheet->getColumnDimension('A')->setWidth(8);
- $sheet->getColumnDimension('B')->setWidth(12);
- $sheet->getColumnDimension('C')->setWidth(30); // 身份证列加宽
- $sheet->getColumnDimension('D')->setWidth(12);
- $sheet->getColumnDimension('E')->setWidth(15);
- $sheet->getColumnDimension('F')->setWidth(25);
- $sheet->getColumnDimension('G')->setWidth(25);
- $sheet->getColumnDimension('H')->setWidth(18);
- // --- 6. 全表加细边框 ---
- $sheet->getStyle("A1:{$lastCol}{$lastRow}")->getBorders()->getAllBorders()->setBorderStyle(Border::BORDER_THIN);
- },
- ];
- }
- }
|