一、安装
1、composer require maatwebsite/excel ~3.1.0
2、在项目下composer.json中require里添加"maatwebsite/excel":"~3.1",并运行composer update 加载该包
二、配置
1、在config/app.php中注册服务提供者到providers数组:Maatwebsite\Excel\ExcelServiceProvider::class,
2、在config/app.php中注册门面到aliases数组 'Excel' => Maatwebsite\Excel\Facades\Excel::class,
3、更多配置php artisan vendor:publish;执行成功会在config目录下生成一个配置文件excel.php。
导出:Excel::download(new ExportExcel($data, $header), "子频道统计$time.xls");
namespace App\Tools;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings; //设置标题
class ExportExcel implements FromArray, WithHeadings
{
protected $data;
protected $header;
/*
* Excel类的构造函数
*/
public function __construct($data, $header)
{
$this->data = $data;
$this->header = $header;
}
// 导出数据逻辑
public function array(): array
{
return $this->data;
}
// 首行标题
public function headings(): array
{
return $this->header;
}
}
导入:Excel::toArray(new ImportExcel, request()->file('excel'));
namespace App\Tools;
use Maatwebsite\Excel\Concerns\ToArray;
class ImportExcel implements ToArray
{
public function Array(Array $tables)
{
return $tables;
}
}