StudentExport.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace App\Exports\School;
  3. use App\Repositories\Enums\ModelStatusEnum;
  4. use App\Repositories\Models\School\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. const BaseFields = [
  18. [
  19. 'name' => '序号',
  20. 'width' => 8,
  21. ],
  22. [
  23. 'name' => '学号',
  24. 'width' => 15,
  25. 'field' => 'account'
  26. ],
  27. [
  28. 'name' => '姓名',
  29. 'width' => 20,
  30. 'field' => 'name'
  31. ],
  32. [
  33. 'name' => '性别',
  34. 'width' => 10,
  35. ],
  36. [
  37. 'name' => '班级',
  38. 'width' => 20,
  39. ],
  40. [
  41. 'name' => '手机号',
  42. 'width' => 20,
  43. 'field' => 'mobile'
  44. ],
  45. [
  46. 'name' => '邮箱',
  47. 'width' => 20,
  48. 'field' => 'email'
  49. ],
  50. [
  51. 'name' => '状态',
  52. 'width' => 10,
  53. ],
  54. ];
  55. protected $rowNums = 0;
  56. protected $ids = [];
  57. protected $lastC = 'A';
  58. protected $fields = [];
  59. public function __construct($ids, $field_ids = [])
  60. {
  61. $this->ids = $ids;
  62. $fields = [];
  63. if (!is_array($field_ids) || !count($field_ids)) {
  64. $fields = self::BaseFields;
  65. } else {
  66. foreach ($field_ids as $field_id) {
  67. $fields[] = self::BaseFields[$field_id];
  68. }
  69. }
  70. $this->fields = $fields;
  71. }
  72. public function query()
  73. {
  74. $ids = $this->ids;
  75. $model = Student::query();
  76. if (!count($ids)) return $model->where('id', 0);
  77. return $model
  78. ->with(['grade'])
  79. ->whereIn('id', $ids)->orderByDesc('id');
  80. }
  81. public function map($row): array
  82. {
  83. $row = $row->toArray();
  84. $this->rowNums++;
  85. $data = [];
  86. foreach ($this->fields as $k => $col) {
  87. $name = $col['name'];
  88. if ($name == '序号') $data[$k] = $this->rowNums;
  89. if (isset($col['field'])) {
  90. $data[$k] = $row[$col['field']];
  91. continue;
  92. }
  93. if ($name == '性别') {
  94. $sex_name = '';
  95. switch ((int)$row['sex']) {
  96. case 1:
  97. $sex_name = '男';
  98. break;
  99. case 2:
  100. $sex_name = '女';
  101. break;
  102. }
  103. $data[$k] = $sex_name;
  104. continue;
  105. }
  106. if ($name == '班级') {
  107. $data[$k] = getVal($row['grade']['name']);
  108. continue;
  109. }
  110. if ($name == '状态') {
  111. $data[$k] = ModelStatusEnum::getDescription($row['status']);
  112. continue;
  113. }
  114. }
  115. return $data;
  116. }
  117. public function headings(): array
  118. {
  119. $room_name = '学生数据';
  120. $data[] = [$room_name];
  121. $data[] = array_column($this->fields, 'name');
  122. return $data;
  123. }
  124. public function headingRow(): int
  125. {
  126. return 2;
  127. }
  128. public function columnWidths(): array
  129. {
  130. $i = 65;
  131. $data = [];
  132. foreach ($this->fields as $k => $field) {
  133. $s = intval($k / 26);
  134. if (!$s) $key = chr($i + $k);
  135. if ($s > 0) {
  136. $y = intval($k % 26);
  137. $key = chr($s + $i - 1);
  138. $key .= chr($y + $i);
  139. }
  140. $data["{$key}"] = $field['width'];
  141. $this->lastC = $key;
  142. }
  143. return $data;
  144. }
  145. public function styles(Worksheet $sheet)
  146. {
  147. $last = $this->lastC;
  148. $cellRange = "A1:{$last}1";
  149. $sheet->getStyle($cellRange)->getFont()->setSize(12);
  150. $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高
  151. // 文字居中
  152. $lastrow = $sheet->getHighestRow();
  153. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
  154. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  155. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setWrapText(true);
  156. $sheet->mergeCells("A1:{$last}1"); //合并
  157. $sheet->getStyle("A1:{$last}1")->getFont()->setSize(16);
  158. }
  159. }