1. 项目的composer.json的require中添加
"maatwebsite/excel": "~2.1.0" 如果是 Laravel 4 则添加 "maatwebsite/excel": "~1.3"
或者项目中执行
composer require maatwebsite/excel ~2.0.0
2, 执行 composer update maatwebsite/excel
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Removing maatwebsite/excel (v1.3.7)
- Installing maatwebsite/excel (v2.1.2)
Downloading: 100%
Writing lock file
Generating autoload files
> php artisan clear-compiled
> php artisan optimize
Generating optimized class loader
Compiling common classes
3,编辑 app/config/app.php
在代码中分别加入
Maatwebsite\Excel\ExcelServiceProvider::class,(providers中的)
'Excel' => Maatwebsite\Excel\Facades\Excel::class,(aliases中的)
4,执行命令 生成配置文件
$ php artisan vendor:publish
Copied File [\vendor\maatwebsite\excel\src\config\excel.php] To [\config\excel.p
hp]
Publishing complete for tag []!
5, 开始用了。
在项目中 先use
use Excel;
代码:
Excel::create('Laravel Excel',function($excel) {$excel->sheet('Excel sheet',function($sheet) {$sheet->setOrientation('landscape'); });})->export('xls');
代码:
$res = $this->Importexcel($path);
var_dump($res);
public function Importexcel($files){
$res = [];
Excel::load($files, function($reader) use( &$res ) {
$reader = $reader->getSheet(0);
$res = $reader->toArray();
});
return $res;
}
网友评论