DemoExport.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Exports;
  3. use App\Repositories\Models\Base\CategorySetting;
  4. use App\Repositories\Models\Model;
  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 DemoExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize, WithColumnWidths, WithStyles
  15. {
  16. use Exportable;
  17. const Name = "证书列表";
  18. const BaseFields = [
  19. [
  20. 'name' => '序号',
  21. 'width' => 8,
  22. ],
  23. [
  24. 'name' => '名称',
  25. 'width' => 30,
  26. 'field' => 'name'
  27. ],
  28. [
  29. 'name' => '编号',
  30. 'width' => 15,
  31. 'field' => 'no'
  32. ],
  33. ];
  34. protected $rowNums = 0;
  35. protected $ids = [];
  36. protected $lastC = 'A';
  37. protected $fields = [];
  38. public function __construct($ids, $field_ids = [])
  39. {
  40. $this->ids = $ids;
  41. $fields = [];
  42. if (!count($field_ids)) {
  43. $fields = self::BaseFields;
  44. } else {
  45. foreach ($field_ids as $field_id) {
  46. $fields[] = self::BaseFields[$field_id];
  47. }
  48. }
  49. $this->fields = $fields;
  50. }
  51. public function query()
  52. {
  53. $ids = $this->ids;
  54. return Model::query()->with(['company'])->whereIn('id', $ids)->orderByDesc('id');
  55. }
  56. public function map($row): array
  57. {
  58. $row = $row->toArray();
  59. $this->rowNums++;
  60. $data = [];
  61. foreach ($this->fields as $k => $item) {
  62. $field = $item['name'];
  63. if (isset($item['field']) && isset($row[$item['field']])) {
  64. $data[$k] = $row[$item['field']];
  65. continue;
  66. }
  67. if ($field == '序号') $data[$k] = $this->rowNums;
  68. if ($field == '证书所属机构') $data[$k] = isset($row['company']['name']) ? $row['company']['name'] : '';
  69. if ($field == '行业分类') {
  70. $data[$k] = arr2str(CategorySetting::byIdsGetName($row['hy_ids']), '、');
  71. }
  72. }
  73. return $data;
  74. }
  75. public function headings(): array
  76. {
  77. $room_name = self::Name;
  78. $data[] = [$room_name];
  79. $data[] = array_column($this->fields, 'name');
  80. return $data;
  81. }
  82. public function headingRow(): int
  83. {
  84. return 2;
  85. }
  86. public function columnWidths(): array
  87. {
  88. $i = 65;
  89. $data = [];
  90. foreach ($this->fields as $k => $field) {
  91. $s = intval($k / 26);
  92. if (!$s) $key = chr($i + $k);
  93. if ($s > 0) {
  94. $y = intval($k % 26);
  95. $key = chr($s + $i - 1);
  96. $key .= chr($y + $i);
  97. }
  98. $data["{$key}"] = $field['width'];
  99. $this->lastC = $key;
  100. }
  101. return $data;
  102. }
  103. public function styles(Worksheet $sheet)
  104. {
  105. $last = $this->lastC;
  106. $cellRange = "A1:{$last}1";
  107. $sheet->getStyle($cellRange)->getFont()->setSize(12);
  108. $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高
  109. // 文字居中
  110. $lastrow = $sheet->getHighestRow();
  111. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
  112. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  113. $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setWrapText(true);
  114. $sheet->mergeCells("A1:{$last}1"); //合并
  115. $sheet->getStyle("A1:{$last}1")->getFont()->setSize(16);
  116. }
  117. }