Lumen框架调用php-excel
1.vim ./composer.json 在require{}添加一行:
"maatwebsite/excel":"~2.1.0"
2.在项目目录下运行:
composer require maatwebsite/excel ~2.1.0
3.在bootstrap文件下的app.PHP中加入:
$app->register(Maatwebsite\Excel\ExcelServiceProvider::class); class_alias('Maatwebsite\Excel\Facades\Excel', 'Excel');
取消下面这2行的注释
$app->withFacades(); $app->withEloquent();
4.修改vendor/maatwebsite/excel/src/Maatwebsite/Excel/ExcelServiceProvider.php 注释掉boot方法里面的 下面几行:
$this->publishes([ __DIR__ . '/../../config/excel.php' => config_path('excel.php'), ]);
修改bindWriters方法里面的
$app->make(Response::class), 为 $app->make('Illuminate\Support\Facades\Response'),
5.调用demo
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Excel; //调用 class ExcelController extends BaseController{ public function export(){ $cellData = [ ['列1','列2','列3'], ['1','AAAAA','aaaaa'], ['2','BBBBB','bbbbb'], ['3','CCCCC','ccccc'], ['4','DDDDD','ddddd'], ['5','EEEEE','eeeee'], ]; Excel::create('demoexcel',function($excel) use ($cellData){ $excel->sheet('demo', function($sheet) use ($cellData){ $sheet->rows($cellData); }); })->export('xls'); } } ?>
使用 store 方法保存excel:
Excel::create('demoexcel',function($excel) use ($cellData){ $excel->sheet('score', function($sheet) use ($cellData){ $sheet->rows($cellData); }); })->store('xls')->export('xls');
文件默认保存到 storage/exports 目录,如果excel包含中文出现乱码,在上面的代码文件名做下面的修改
iconv('UTF-8', 'GBK', 'demoexcel')
原创文章如转载,请注明出处,本文首发于彭超的博客
文章版权声明:除非注明,否则均为彭超的博客原创文章,转载或复制请以超链接形式并注明出处。
- 评论列表:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。