Laravel集成Maatwebsite-Laravel-Excel最新版本v3
github:https://github.com/Maatwebsite/Laravel-Excel
參考文檔:https://docs.laravel-excel.com/3.1/getting-started/installation.html
1). 使用 composer 安裝:
此處下載的是最新版,目前是 v3.1.18,PHP版本要求大于 7.2 ,而v3和v2是不兼容的,方法還不一樣,完全是重寫了,網上看到的教程基本都是v2的,我
覺得作者舍棄老版本肯定是有原因的,還是不要固執的使用v2啦。
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示例:數組模型導出
<?phpnamespace 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 <?phpnamespace 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,因此具備可改造的能力。
那么應該在某個地方使用了 Collection::mixin() 或者 Collection::macro() 來為 Collection 賦能,仔細想想
此Excel插件和Laravel框架的結合點,我們只是配置了providers,那么我們去看Illuminate\View\ViewServiceProvider
在register方法中:
可見它給Collection類注入了兩個對象,那么這兩個對象的所有方法都可以被Collection對象使用。實際上它們都只是提供了一個
方法,都返回閉包,分別是:
其實就是在閉包內創建了導出數據模型,而且都是 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() 方法 也是一樣的操作。
總結
以上是生活随笔為你收集整理的Laravel集成Maatwebsite-Laravel-Excel最新版本v3的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 弹性盒模型(详解)
- 下一篇: NAND Flash ECC算法长度计算