123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace App\Exports\BikeManage;
- use App\Repositories\Enums\BikeManage\BindingStatusEnum;
- use App\Repositories\Models\BikeManage\Bike;
- use Maatwebsite\Excel\Concerns\Exportable;
- use Maatwebsite\Excel\Concerns\FromQuery;
- use Maatwebsite\Excel\Concerns\ShouldAutoSize;
- use Maatwebsite\Excel\Concerns\WithColumnWidths;
- use Maatwebsite\Excel\Concerns\WithHeadings;
- use Maatwebsite\Excel\Concerns\WithMapping;
- use Maatwebsite\Excel\Concerns\WithStyles;
- use PhpOffice\PhpSpreadsheet\Style\Alignment;
- use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
- class BikeCodeExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize, WithColumnWidths, WithStyles
- {
- use Exportable;
- const BaseFields = [
- [
- 'name' => '序号',
- 'width' => 8,
- ],
- [
- 'name' => '校内牌照',
- 'width' => 15,
- ],
- [
- 'name' => '车辆牌照',
- 'width' => 15,
- ],
- [
- 'name' => '二维码数据',
- 'width' => 50,
- ],
- [
- 'name' => '是否绑定',
- 'width' => 30,
- ],
- [
- 'name' => '车主姓名',
- 'width' => 15,
- ],
- [
- 'name' => '车主手机号',
- 'width' => 15,
- ],
- [
- 'name' => '车主班级',
- 'width' => 10,
- ],
- ];
- protected $rowNums = 0;
- protected $ids = [];
- protected $lastC = 'A';
- protected $fields = [];
- public function __construct($ids, $field_ids = [])
- {
- $this->ids = $ids;
- $fields = [];
- if (!count($field_ids)) {
- $fields = self::BaseFields;
- } else {
- foreach ($field_ids as $field_id) {
- $fields[] = self::BaseFields[$field_id];
- }
- }
- $this->fields = $fields;
- }
- public function query()
- {
- return Bike::query()
- ->with(['user'])
- ->whereIn('id', $this->ids)
- ->orderByDesc('id');
- }
- public function map($row): array
- {
- $row = $row->toArray();
- $this->rowNums++;
- $data = [];
- foreach (array_column($this->fields, 'name') as $k => $field) {
- if ($field == '序号') $data[$k] = $this->rowNums;
- if ($field == '校内牌照') $data[$k] = $row['school_no'];
- if ($field == '车辆牌照') $data[$k] = $row['bike_no'];
- if ($field == '是否绑定') $data[$k] = BindingStatusEnum::getDescription($row['is_bind']);
- if ($field == '二维码数据') $data[$k] = $row['qr_cord'];
- if ($field == '车主姓名') $data[$k] = $row['user'] ? $row['user']['name'] : '';
- if ($field == '车主手机号') $data[$k] = $row['user'] ? $row['user']['mobile'] : '';
- if ($field == '车主班级') $data[$k] = $row['user'] ? $row['user']['class_name'] : '';
- }
- return $data;
- }
- public function headings(): array
- {
- $room_name = '车辆列表';
- $data[] = [$room_name];
- $data[] = array_column($this->fields, 'name');
- return $data;
- }
- public function headingRow(): int
- {
- return 2;
- }
- public function columnWidths(): array
- {
- $i = 65;
- $data = [];
- foreach ($this->fields as $k => $field) {
- $s = intval($k / 26);
- if (!$s) $key = chr($i + $k);
- if ($s > 0) {
- $y = intval($k % 26);
- $key = chr($s + $i - 1);
- $key .= chr($y + $i);
- }
- $data["{$key}"] = $field['width'];
- $this->lastC = $key;
- }
- return $data;
- }
- public function styles(Worksheet $sheet)
- {
- $last = $this->lastC;
- $cellRange = "A1:{$last}1";
- $sheet->getStyle($cellRange)->getFont()->setSize(12);
- $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高
- // 文字居中
- $lastrow = $sheet->getHighestRow();
- $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
- $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
- $sheet->getStyle("A1:{$last}" . $lastrow)->getAlignment()->setWrapText(true);
- $sheet->mergeCells("A1:{$last}1"); //合并
- $sheet->getStyle("A1:{$last}1")->getFont()->setSize(16);
- }
- }
|