程序中经常要用到EXCEL表格操作。 maatwebsite/excel是一个不错的程序包。
项目地址
https://github.com/Maatwebsite/Laravel-Excel
官方文档:
http://www.maatwebsite.nl/laravel-excel/docs/getting-started#installation
安装方法。
1, 项目的composer.json 中添加
"maatwebsite/excel": "~2.1.0" 如果是 Laravel 4 则添加 "maatwebsite/excel": "~1.3" (确信版本正确,勿入坑,以下都是laravel 5步骤)
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,
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
4,执行命令 生成配置文件
$ php artisan vendor:publish
Copied File [\vendor\maatwebsite\excel\src\config\excel.php] To [\config\excel.p
hp]
Publishing complete for tag []!
$ php artisan vendor:publish
Copied File [\vendor\maatwebsite\excel\src\config\excel.php] To [\config\excel.p
hp]
Publishing complete for tag []!
6, 开始用了。
在项目中 先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;
}
7,还是看官方文档吧。
http://www.maatwebsite.nl/laravel-excel/docs/import