日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Laravel集成Maatwebsite-Laravel-Excel最新版本v3

發(fā)布時間:2024/1/1 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Laravel集成Maatwebsite-Laravel-Excel最新版本v3 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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是不兼容的,方法還不一樣,完全是重寫了,網(wǎng)上看到的教程基本都是v2的,我
    覺得作者舍棄老版本肯定是有原因的,還是不要固執(zhí)的使用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 數(shù)組內

    'providers' => [...Maatwebsite\Excel\ExcelServiceProvider::class, ]注冊Facade'aliases' => [...'Excel' => Maatwebsite\Excel\Facades\Excel::class, ]

    3). 發(fā)布配置

    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)*/

    創(chuàng)建一個導出數(shù)據(jù)模型類,來作為數(shù)據(jù)源。

    php artisan make:export UsersExport

    示例:數(shù)組模型導出

    <?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'); }

    執(zhí)行 php artisan DemoCommand

    于是,在 storage/app 下生成了文件 invoices.xlsx

    1、diskName參數(shù)是需要指定Laravel文件系統(tǒng)Filesystem的磁盤位置,并不是我們的物理磁盤。2、filePath參數(shù)是相對于storage/app下的路徑。3、目前來看,值為0的導出后是空的,如果需要顯示0的話,那么你的模型要實現(xiàn)接口WithStrictNullComparison。4、默認起始單元格式A1,如果想修改,需要模型實現(xiàn)接口WithCustomStartCell。需要實現(xiàn)方法public function startCell(): string{return 'B2';}目前WithCustomStartCell只支持collection模型。5、設置sheet的名稱,需要你的導出模型實現(xiàn)接口WithTitle的方法:public function title(): string{return '導出數(shù)據(jù)';}6、多sheet表需要實現(xiàn)接口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'); }

    數(shù)組轉集合:

    new Collection(array) collect(array)

    示例:返回原始的二進制數(shù)據(jù)

    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支持

    如果你以為必須要先建立導出數(shù)據(jù)模型那你就太低估這個插件了,其實它還提供了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()

    其實就是在閉包內創(chuàng)建了導出數(shù)據(jù)模型,而且都是 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模型,但是并沒有實現(xiàn)接口 WithStrictNullComparison 和 WithCustomStartCell。

    那么對于數(shù)組模型,只需要將數(shù)組轉成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的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。