ProjectStaffExport.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\Style\Color;
  9. class ProjectStaffExport 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 = 'I';
  26. $dataCount = count($this->data);
  27. $lastRow = 2 + $dataCount;
  28. // --- 1. 第一行:主标题(包含红色提示文字) ---
  29. $sheet->mergeCells('A1:I1');
  30. $sheet->setCellValue('A1', "项目研发活动人员情况表");
  31. $sheet->getStyle('A1')->getFont()->setBold(true)->setSize(14);
  32. // --- 2. 第二行:表头设置 ---
  33. $headers = [
  34. 'A' => '序号',
  35. 'B' => '姓名',
  36. 'C' => '学历',
  37. 'D' => '专业',
  38. 'E' => '职称',
  39. 'F' => "项目", // 提示列
  40. 'G' => '项目角色',
  41. 'H' => '部门',
  42. 'I' => '承担职责'
  43. ];
  44. foreach ($headers as $col => $title) {
  45. $sheet->setCellValue("{$col}2", $title);
  46. }
  47. // 设置表头中 F2 单元格“项目角色”下方文字为红色
  48. $sheet->getStyle('F2')->getFont()->getColor()->setARGB(Color::COLOR_RED);
  49. $sheet->getStyle('A2:I2')->getFont()->setBold(true);
  50. // --- 3. 填充数据 ---
  51. $currentRow = 3;
  52. foreach ($this->data as $index => $row) {
  53. $sheet->fromArray([
  54. $index + 1,
  55. $row['title'] ?? '',
  56. $row['education'] ?? '',
  57. $row['major'] ?? '',
  58. $row['p_title'] ?? '',
  59. $row['item_title'] ?? '',
  60. $row['item_role'] ?? '',
  61. $row['depart_title'] ?? '',
  62. $row['duty'] ?? '',
  63. ], null, "A{$currentRow}");
  64. $currentRow++;
  65. }
  66. // --- 4. 全局样式 ---
  67. $fullRange = "A1:{$lastCol}{$lastRow}";
  68. $sheet->getStyle($fullRange)->getAlignment()->applyFromArray([
  69. 'vertical' => Alignment::VERTICAL_CENTER,
  70. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  71. 'wrapText' => true,
  72. ]);
  73. // --- 5. 行高与列宽 ---
  74. $sheet->getRowDimension(1)->setRowHeight(35);
  75. $sheet->getRowDimension(2)->setRowHeight(45); // 表头双行文字
  76. $sheet->getColumnDimension('A')->setWidth(8);
  77. $sheet->getColumnDimension('B')->setWidth(12);
  78. $sheet->getColumnDimension('F')->setWidth(20); // 角色定义列
  79. $sheet->getColumnDimension('G')->setWidth(15);
  80. $sheet->getColumnDimension('H')->setWidth(15);
  81. $sheet->getColumnDimension('I')->setWidth(25); // 职责列
  82. // --- 6. 边框 ---
  83. $sheet->getStyle("A2:{$lastCol}{$lastRow}")->getBorders()->getAllBorders()->setBorderStyle(Border::BORDER_THIN);
  84. },
  85. ];
  86. }
  87. }