https://github.com/Maatwebsite/Laravel-Excel
首先在Laravel項目根目錄下使用Composer安裝依賴:
composer require maatwebsite/excel ~2.0.0
安裝后的設(shè)置
在 config/app.php中注冊服務(wù)提供者到 providers數(shù)組:
Maatwebsite\Excel\ExcelServiceProvider::class,
同樣在 config/app.php 中注冊門面到 aliases數(shù)組:
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
如果想要對Laravel Excel進行更多的自定義配置,執(zhí)行如下Artisan命令:
php artisan vendor:publish
執(zhí)行成功后會在 config目錄下生成一個配置文件 excel.php。
例子:
use DB,Excel;
foreach($cards as $v){
$charge=$v->is_used?'已兌換':'未兌換';
$data[]=[
$v->card_no.'',
$v->card_pwd,
$v->card->card_name,
$v->card->price,
$charge,
$v->card->expired_at
];
}
Excel::create('Excel導出數(shù)據(jù)',function($excel) use ($data){
$excel->sheet('數(shù)據(jù)', function($sheet) use ($data){
$sheet->rows($data);
});
})->export('xls');