主要实现FromArray接口类、WithHeadings接口类、WithEvents接口类的对应函数
1、导出类
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
class ExampleExporter implements FromArray, WithHeadings, WithEvents
{
private $data;
public function __construct (array $data)
{
$this->data = $data;
}
public function array() : array
{
return $this->data;
}
// 列名
public function headings() : array
{
return [
'列名1',
'列名1',
'列名2'
];
}
public function registerEvents() : array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$event->sheet->getDelegate()->getColumnDimension('A')->setWidth(1