StudentExport.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Exports;
  3. use App\Repositories\Enums\SexEnum;
  4. use App\Repositories\Models\User\Student;
  5. use Maatwebsite\Excel\Concerns\Exportable;
  6. use Maatwebsite\Excel\Concerns\FromQuery;
  7. use Maatwebsite\Excel\Concerns\ShouldAutoSize;
  8. use Maatwebsite\Excel\Concerns\WithColumnWidths;
  9. use Maatwebsite\Excel\Concerns\WithHeadings;
  10. use Maatwebsite\Excel\Concerns\WithMapping;
  11. use Maatwebsite\Excel\Concerns\WithStyles;
  12. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  13. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  14. class StudentExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize, WithColumnWidths, WithStyles
  15. {
  16. use Exportable;
  17. protected $rowNums = 0;
  18. protected $ids = [];
  19. protected $fields = [];
  20. public function __construct(array $fields)
  21. {
  22. $this->fields = $fields;
  23. }
  24. public function forIds(array $ids)
  25. {
  26. $this->ids = $ids;
  27. return $this;
  28. }
  29. public function query()
  30. {
  31. return Student::query()->whereIn('id', $this->ids);
  32. }
  33. public function headings(): array
  34. {
  35. $bases = [
  36. '序号',
  37. '姓名',
  38. '性别',
  39. '联系电话',
  40. '届',
  41. '院系',
  42. '专业',
  43. '学号(账号)',
  44. '密码',
  45. ];
  46. return $bases;
  47. // $data[] = '序号';
  48. // foreach ($this->fields as $field) {
  49. // $data[] = $bases[$field];
  50. // }
  51. //
  52. // return $data;
  53. }
  54. public function columnWidths(): array
  55. {
  56. $bases = [
  57. 'A' => 8,
  58. 'B' => 30,
  59. 'C' => 10,
  60. 'D' => 30,
  61. 'E' => 15,
  62. 'F' => 40,
  63. 'G' => 40,
  64. 'H' => 30,
  65. 'I' => 30,
  66. // 'J' => 10,
  67. // 'K' => 15,
  68. // 'L' => 15,
  69. // 'M' => 15,
  70. // 'N' => 15,
  71. // 'O' => 15,
  72. // 'P' => 15,
  73. // 'Q' => 30,
  74. // 'R' => 15,
  75. // 'S' => 8,
  76. // 'T' => 15,
  77. // 'U' => 20,
  78. // 'V' => 15,
  79. ];
  80. $data['A'] = 8;
  81. $values = array_values($bases);
  82. $keys = array_keys($bases);
  83. foreach ($this->fields as $field) {
  84. $key = $keys[$field];
  85. $data["{$key}"] = $values[$field];
  86. }
  87. return $data;
  88. }
  89. public function styles(Worksheet $sheet)
  90. {
  91. $cellRange = 'A1:I1';
  92. $sheet->getStyle($cellRange)->getFont()->setSize(12);
  93. $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高
  94. // 文字居中
  95. $lastrow = $sheet->getHighestRow();
  96. $sheet->getStyle('A1:I' . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
  97. $sheet->getStyle('A1:I' . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  98. $sheet->getStyle('A1:I' . $lastrow)->getAlignment()->setWrapText(true);
  99. }
  100. public function map($row): array
  101. {
  102. $this->rowNums++;
  103. $bases = [
  104. $this->rowNums,
  105. $row['turename'],
  106. SexEnum::getDescription($row['sex']),
  107. $row['mobile'] . ' ',
  108. $row['period'],
  109. $row['faculty'],
  110. $row['class'],
  111. $row['account'],
  112. $row['password']
  113. ];
  114. return $bases;
  115. // $data[] = $this->rowNums;
  116. // foreach ($this->fields as $field) {
  117. // $data[] = $bases[$field];
  118. // }
  119. // return $data;
  120. }
  121. }