牛骨文教育服务平台(让学习变的简单)
博文笔记

laravel 的EXCEL读写 maatwebsite/excel

创建时间:2016-04-16 投稿人: 浏览次数:10531

程序中经常要用到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
    在代码中分别加入
        MaatwebsiteExcelExcelServiceProvider::class,


       "Excel"    => MaatwebsiteExcelFacadesExcel::class,

4,执行命令  生成配置文件
$ php artisan vendor:publish
Copied File [vendormaatwebsiteexcelsrcconfigexcel.php] To [configexcel.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

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。