laravel 的EXCEL读写 maatwebsite/excel

程序中经常要用到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 []!


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

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值