UserExport.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Exports;
  3. use App\Repositories\Enums\SexEnum;
  4. use App\Repositories\Models\User\User;
  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 UserExport 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 User::query()->whereIn('id', $this->ids);
  32. }
  33. public function headings(): array
  34. {
  35. $bases = [
  36. '序号',
  37. '账号',
  38. '姓名',
  39. '性别',
  40. '联系电话',
  41. '专业',
  42. ];
  43. return $bases;
  44. }
  45. public function columnWidths(): array
  46. {
  47. $bases = [
  48. 'A' => 8,
  49. 'B' => 30,
  50. 'C' => 30,
  51. 'D' => 10,
  52. 'E' => 40,
  53. 'F' => 50,
  54. // 'G' => 40,
  55. // 'H' => 30,
  56. // 'I' => 30,
  57. // 'J' => 10,
  58. // 'K' => 15,
  59. // 'L' => 15,
  60. // 'M' => 15,
  61. // 'N' => 15,
  62. // 'O' => 15,
  63. // 'P' => 15,
  64. // 'Q' => 30,
  65. // 'R' => 15,
  66. // 'S' => 8,
  67. // 'T' => 15,
  68. // 'U' => 20,
  69. // 'V' => 15,
  70. ];
  71. $data['A'] = 8;
  72. $values = array_values($bases);
  73. $keys = array_keys($bases);
  74. foreach ($this->fields as $field) {
  75. $key = $keys[$field];
  76. $data["{$key}"] = $values[$field];
  77. }
  78. return $data;
  79. }
  80. public function styles(Worksheet $sheet)
  81. {
  82. $cellRange = 'A1:I1';
  83. $sheet->getStyle($cellRange)->getFont()->setSize(12);
  84. $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高
  85. // 文字居中
  86. $lastrow = $sheet->getHighestRow();
  87. $sheet->getStyle('A1:I' . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
  88. $sheet->getStyle('A1:I' . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  89. $sheet->getStyle('A1:I' . $lastrow)->getAlignment()->setWrapText(true);
  90. }
  91. public function map($row): array
  92. {
  93. $this->rowNums++;
  94. $bases = [
  95. $this->rowNums,
  96. $row['username'],
  97. $row['name'],
  98. SexEnum::getDescription($row['sex']),
  99. $row['mobile'] . ' ',
  100. $row['class'],
  101. ];
  102. return $bases;
  103. // $data[] = $this->rowNums;
  104. // foreach ($this->fields as $field) {
  105. // $data[] = $bases[$field];
  106. // }
  107. // return $data;
  108. }
  109. }