github:https://github.com/Maatwebsite/Laravel-Excel
参考文档:https://docs.laravel-excel.com/3.1/getting-started/installation.html
- 安装
1). 使用 composer 安装:
composer require maatwebsite/excel
此处下载的是最新版,目前是 v3.1.18,PHP版本要求大于 7.2 ,而v3和v2是不兼容的,方法还不一样,完全是重写了,网上看到的教程基本都是v2的,我
觉得作者舍弃老版本肯定是有原因的,还是不要固执的使用v2啦。
Version Laravel Version Php Version Support
2.1 <=5.6 <=7.0 Unsupported since 15-5-2018
3.0 ^5.5 ^7.0 Unsupported since 31-12-2018
3.1 ^5.5|^6.0 ^7.1 New features
2). 安装完成后,修改 config/app.php 在 providers 数组内
'providers' => [
...
Maatwebsite\Excel\ExcelServiceProvider::class,
]
注册Facade
'aliases' => [
...
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]
3). 发布配置
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
在 config 目录下多出了 excel.php
- 使用
全部方法:
/**
* @method static BinaryFileResponse download(object $export, string $fileName, string $writerType = null, array $headers = [])
* @method static bool store(object $export, string $filePath, string $disk = null, string $writerType = null, $diskOptions = [])
* @method static PendingDispatch queue(object $export, string $filePath, string $disk = null, string $writerType = null, $diskOptions = [])
* @method static BaseExcel import(object $import, string $filePath, string $disk = null, string $readerType = null)
* @method static array toArray(object $import, string $filePath, string $disk = null, string $readerType = null)
* @method static Collection toCollection(object $import, string $filePath, string $disk = null, string $readerType = null)
* @method static PendingDispatch queueImport(object $import, string $filePath, string $disk = null, string $readerType = null)
* @method static void matchByRegex()
* @method static void doNotMatchByRegex()
* @method static void assertDownloaded(string $fileName, callable $callback = null)
* @method static void assertStored(string $filePath, string $disk = null, callable $callback = null)
* @method static void assertQueued(string $filePath, string $disk = null, callable $callback = null)
* @method static void assertImported(string $filePath, string $disk = null, callable $callback = null)
*/
创建一个导出数据模型类,来作为数据源。
php artisan make:export UsersExport
示例:数组模型导出
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromArray;
class UsersExport implements FromArray
{
protected $invoices;
public function __construct(array $invoices)
{
$this->invoices = $invoices;
}
public function array(): array
{
return $this->invoices;
}
}
我们可以通过http请求来下载download,也可以直接生成并保存到本地store
我是喜欢用命令行来测试,于是我在 DemoCommand 中:
use Maatwebsite\Excel\Facades\Excel;
public function handle()
{
$this->test6();
}
function test6()
{
$cellData = [
['学号', '姓名', '成绩'],
['10001', 'AAAAA', '99'],
['10002', 'BBBBB', '92'],
['10003', 'CCCCC', '95'],
['10004', 'DDDDD', '89'],
['10005', 'EEEEE', '96'],
];
$export = new UsersExport($cellData);
Excel::store($export, 'invoices.xlsx');
}
执行 php artisan DemoCommand
于是,在 storage/app 下生成了文件 invoices.xlsx
1、diskName参数是需要指定Laravel文件系统Filesystem的磁盘位置,并不是我们的物理磁盘。
2、filePath参数是相对于storage/app下的路径。
3、目前来看,值为0的导出后是空的,如果需要显示0的话,那么你的模型要实现接口WithStrictNullComparison。
4、默认起始单元格式A1,如果想修改,需要模型实现接口WithCustomStartCell。需要实现方法
public function startCell(): string
{
return 'B2';
}
目前WithCustomStartCell只支持collection模型。
5、设置sheet的名称,需要你的导出模型实现接口WithTitle的方法:
public function title(): string
{
return '导出数据';
}
6、多sheet表需要实现接口WithMultipleSheets,具体看文档。
示例:collection模型导出
php artisan make:export CollectionExport
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
class CollectionExport implements FromCollection, WithStrictNullComparison
{
private $collections;
public function __construct($collections)
{
$this->collections = $collections;
}
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return $this->collections;
}
}
当然,你还可以在CollectionExport中对集合做一些处理。
在 DemoCommand 中:
public function handle()
{
$this->test7();
}
function test7(){
$coll = MapCity::take(10)->get();
Excel::store(new CollectionExport($coll), 'map/collection.xlsx');
}
数组转集合:
new Collection(array)
collect(array)
示例:返回原始的二进制数据
function test7(){
$cellData = [
['学号', '姓名', '成绩'],
['10001', 'AAAAA', '99'],
['10002', 'BBBBB', '92'],
['10003', 'CCCCC', '95'],
['10004', 'DDDDD', '89'],
['10005', 'EEEEE', '96'],
];
$export = new UsersExport($cellData);
$contents = Excel::raw($export, \Maatwebsite\Excel\Excel::XLSX);// Excel::XLSX 会提示未定义
file_put_contents('aa.xlsx', $contents);
}
3、macro支持
如果你以为必须要先建立导出数据模型那你就太低估这个插件了,其实它还提供了macro支持,为collection对象赋能。
关于macro的原理可参考:https://blog.csdn.net/raoxiaoya/article/details/103897235
首先看 Illuminate\Support\Collection
类:
引入了Macroable,因此具备可改造的能力。
use Macroable; // Illuminate\Support\Traits\Macroable
那么应该在某个地方使用了 Collection::mixin()
或者 Collection::macro()
来为 Collection 赋能,仔细想想
此Excel插件和Laravel框架的结合点,我们只是配置了providers,那么我们去看Illuminate\View\ViewServiceProvider
在register方法中:
Collection::mixin(new DownloadCollection);
Collection::mixin(new StoreCollection);
可见它给Collection类注入了两个对象,那么这两个对象的所有方法都可以被Collection对象使用。实际上它们都只是提供了一个
方法,都返回闭包,分别是:
downloadExcel()
storeExcel()
其实就是在闭包内创建了导出数据模型,而且都是 collection模型,比如:
public function storeExcel()
{
return function (string $filePath, string $disk = null, string $writerType = null, $withHeadings = false) {
$export = new class($this, $withHeadings) implements FromCollection, WithHeadings {
...
};
return $export->store($filePath, $disk, $writerType);
};
}
可以看到,虽然是collection模型,但是并没有实现接口 WithStrictNullComparison
和 WithCustomStartCell
。
那么对于数组模型,只需要将数组转成collection就可以了。
示例:
function test7(){
$coll = MapCity::take(10)->get();
$coll->storeExcel('map/collection2.xlsx');
$cellData = [
['学号', '姓名', '成绩'],
['10001', 'AAAAA', '99'],
['10002', 'BBBBB', '92'],
['10003', 'CCCCC', '95'],
['10004', 'DDDDD', '89'],
['10005', 'EEEEE', '96'],
];
(new Collection($cellData))->storeExcel('map/array2.xlsx');
}
对于 downloadExcel() 方法 也是一样的操作。