123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- namespace App\Exports;
- use App\Repositories\Enums\SexEnum;
- use App\Repositories\Models\User\Student;
- 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 StudentExport implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize, WithColumnWidths, WithStyles
- {
- use Exportable;
- protected $rowNums = 0;
- protected $ids = [];
- protected $fields = [];
- public function __construct(array $fields)
- {
- $this->fields = $fields;
- }
- public function forIds(array $ids)
- {
- $this->ids = $ids;
- return $this;
- }
- public function query()
- {
- return Student::query()->whereIn('id', $this->ids);
- }
- public function headings(): array
- {
- $bases = [
- '序号',
- '姓名',
- '性别',
- '联系电话',
- '届',
- '院系',
- '专业',
- '学号(账号)',
- '密码',
- ];
- return $bases;
- // $data[] = '序号';
- // foreach ($this->fields as $field) {
- // $data[] = $bases[$field];
- // }
- //
- // return $data;
- }
- public function columnWidths(): array
- {
- $bases = [
- 'A' => 8,
- 'B' => 30,
- 'C' => 10,
- 'D' => 30,
- 'E' => 15,
- 'F' => 40,
- 'G' => 40,
- 'H' => 30,
- 'I' => 30,
- // 'J' => 10,
- // 'K' => 15,
- // 'L' => 15,
- // 'M' => 15,
- // 'N' => 15,
- // 'O' => 15,
- // 'P' => 15,
- // 'Q' => 30,
- // 'R' => 15,
- // 'S' => 8,
- // 'T' => 15,
- // 'U' => 20,
- // 'V' => 15,
- ];
- $data['A'] = 8;
- $values = array_values($bases);
- $keys = array_keys($bases);
- foreach ($this->fields as $field) {
- $key = $keys[$field];
- $data["{$key}"] = $values[$field];
- }
- return $data;
- }
- public function styles(Worksheet $sheet)
- {
- $cellRange = 'A1:I1';
- $sheet->getStyle($cellRange)->getFont()->setSize(12);
- $sheet->getDefaultRowDimension()->setRowHeight(40);//设置行高
- // 文字居中
- $lastrow = $sheet->getHighestRow();
- $sheet->getStyle('A1:I' . $lastrow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);//垂直居中
- $sheet->getStyle('A1:I' . $lastrow)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
- $sheet->getStyle('A1:I' . $lastrow)->getAlignment()->setWrapText(true);
- }
- public function map($row): array
- {
- $this->rowNums++;
- $bases = [
- $this->rowNums,
- $row['turename'],
- SexEnum::getDescription($row['sex']),
- $row['mobile'] . ' ',
- $row['period'],
- $row['faculty'],
- $row['class'],
- $row['account'],
- $row['password']
- ];
- return $bases;
- // $data[] = $this->rowNums;
- // foreach ($this->fields as $field) {
- // $data[] = $bases[$field];
- // }
- // return $data;
- }
- }
|