创建文件
php artisan make:export ProjectExport --model=模型名
<?php
namespace App\Exports;
use App\Models\Company;
use Illuminate\Contracts\Support\Responsable;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
class ProjectExport implements FromArray, WithMapping, WithHeadings, Responsable
{
public function collection()
{
return Company::all();
}
use Exportable;
protected $data;
private $fileName;
public function __construct($data)
{
$this->data = $data; // 实例化该脚本的时候,需要传入要导出的数据
$this->fileName = date('YmdHis') . '_Financial_project.xlsx'; // 指定导出的文件名
}
public function array(): array // 返回的数据
{
return $this->data;
}
public function map($value): array // 指定excel中每一列的数据字段
{
//导出字段
return [
$value['company'],
$value['code'],
$value['industry'],
];
}
public function headings(): array // 指定excel的表头
{
//表头
return [
'名称',
'分类',
'兴业',
];
}
}
控制器调用
public function exportLists(Request $request)
{
$data = Company::get()->toArray();// 查询要导出的数据
return Excel::download(new ProjectExport($data),'企业.xlsx');
}
laravel8 导出excel
最新推荐文章于 2024-04-07 10:39:48 发布