TechnicalStaffExport.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Exports;
  3. use Maatwebsite\Excel\Concerns\WithEvents;
  4. use Maatwebsite\Excel\Concerns\WithTitle;
  5. use Maatwebsite\Excel\Events\AfterSheet;
  6. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  7. use PhpOffice\PhpSpreadsheet\Style\Border;
  8. use PhpOffice\PhpSpreadsheet\Cell\DataType;
  9. class TechnicalStaffExport implements WithEvents, WithTitle
  10. {
  11. protected $data;
  12. public function __construct(array $data)
  13. {
  14. $this->data = $data;
  15. }
  16. public function title(): string
  17. {
  18. return '科技人员情况表';
  19. }
  20. public function registerEvents(): array
  21. {
  22. return [
  23. AfterSheet::class => function (AfterSheet $event) {
  24. $sheet = $event->sheet->getDelegate();
  25. $lastCol = 'H';
  26. $dataCount = count($this->data);
  27. $lastRow = 2 + $dataCount;
  28. // --- 1. 第一行:主标题 ---
  29. $sheet->mergeCells('A1:H1');
  30. $sheet->setCellValue('A1', "企业研究开发科技人员情况表");
  31. $sheet->getStyle('A1')->getFont()->setBold(true)->setSize(16);
  32. // --- 2. 第二行:表头 ---
  33. $headers = [
  34. 'A' => '序号',
  35. 'B' => '姓名',
  36. 'C' => '身份证号码',
  37. 'D' => '学历',
  38. 'E' => '专业',
  39. 'F' => '职称/职业资格',
  40. 'G' => '部门/岗位',
  41. 'H' => '聘用类型',
  42. ];
  43. foreach ($headers as $col => $title) {
  44. $sheet->setCellValue("{$col}2", $title);
  45. }
  46. $sheet->getStyle('A2:H2')->getFont()->setBold(true);
  47. // --- 3. 填充数据 (解决科学计数法关键) ---
  48. $currentRow = 3;
  49. foreach ($this->data as $index => $row) {
  50. // 序号、姓名正常填充
  51. $sheet->setCellValue("A{$currentRow}", $index + 1);
  52. $sheet->setCellValue("B{$currentRow}", $row['name'] ?? '');
  53. // 【关键点】显式设置身份证列为文本格式 (Explicitly set as String)
  54. $idCard = $row['id_card'] ?? '';
  55. $sheet->setCellValueExplicit("C{$currentRow}", $idCard, DataType::TYPE_STRING);
  56. // 其他字段
  57. $sheet->setCellValue("D{$currentRow}", $row['education'] ?? '');
  58. $sheet->setCellValue("E{$currentRow}", $row['major'] ?? '');
  59. $sheet->setCellValue("F{$currentRow}", $row['title_level'] ?? '');
  60. $sheet->setCellValue("G{$currentRow}", $row['department_job'] ?? '');
  61. $sheet->setCellValue("H{$currentRow}", $row['employment_type'] ?? '');
  62. $currentRow++;
  63. }
  64. // --- 4. 样式全局精修 ---
  65. $fullRange = "A1:{$lastCol}{$lastRow}";
  66. $sheet->getStyle($fullRange)->getAlignment()->applyFromArray([
  67. 'vertical' => Alignment::VERTICAL_CENTER,
  68. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  69. 'wrapText' => true,
  70. ]);
  71. // --- 5. 设置行高与列宽 ---
  72. $sheet->getRowDimension(1)->setRowHeight(40);
  73. $sheet->getRowDimension(2)->setRowHeight(30);
  74. $sheet->getColumnDimension('A')->setWidth(8);
  75. $sheet->getColumnDimension('B')->setWidth(12);
  76. $sheet->getColumnDimension('C')->setWidth(30); // 身份证列加宽
  77. $sheet->getColumnDimension('D')->setWidth(12);
  78. $sheet->getColumnDimension('E')->setWidth(15);
  79. $sheet->getColumnDimension('F')->setWidth(25);
  80. $sheet->getColumnDimension('G')->setWidth(25);
  81. $sheet->getColumnDimension('H')->setWidth(18);
  82. // --- 6. 全表加细边框 ---
  83. $sheet->getStyle("A1:{$lastCol}{$lastRow}")->getBorders()->getAllBorders()->setBorderStyle(Border::BORDER_THIN);
  84. },
  85. ];
  86. }
  87. }