讨论数量:
<?php
namespace App\Exports;
use App\Models\User;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
class UsersExport implements FromQuery,WithHeadings,WithMapping
{
public function __construct($where){
$this->where = $where;
}
//laravel Excel的分块处理
public function query()
{
$where = $this->where;
return User::query()->where($where);
}
//标题
public function headings(): array
{
$header = [
'name'=>'姓名',
'job'=>'职务',
'craft'=>'工种',
'politics_status'=>'政治面貌',
'phone'=>'联系电话',
];
return $header;
}
//处理数据
public function map($row): array
{
$data = [
'name'=>$row->name,
'job'=>$row->job,
'craft'=>$row->craft,
'politics_status'=>$row->politics_status,
'phone'=>$row->phone,
];
return $data;
}
}
贴上实例,控制器里面调用还是一样的 return Excel::download(new UsersExport($data), ‘invoices.xlsx’);
推荐文章: