php larver 导出e,laravel5 Excel导出

1、安装:maatwebsite/excel的2.1.0版本,命令行:composer require "maatwebsite/excel:~2.1.0";

6683

6683

2、在config/app.php中,注册服务提供者和注册门面:'providers' => [

/*

* 注册服务提供者

* Laravel Framework Service Providers...

*/

Illuminate\Auth\AuthServiceProvider::class,

... ...

App\Providers\RouteServiceProvider::class,

//Excel导入导出

Maatwebsite\Excel\ExcelServiceProvider::class,

],'aliases' => [

//注册门面

'App' => Illuminate\Support\Facades\App::class,

'Artisan' => Illuminate\Support\Facades\Artisan::class,

... ...

'View' => Illuminate\Support\Facades\View::class,

'Excel' => Maatwebsite\Excel\Facades\Excel::class,

],

3、添加路由$router->group([

'prefix' => 'excel',

'namespace' => 'Excel',

], function(Router $router) {

//导出用户列表

$router->get('/excelExports', "ExcelController@excelExport");

});

4、创建控制器,命令:php artisan make:controller Admin/Excel/ExcelController ,并复制以下代码:<?php

namespace App\Http\Controllers\Admin\Excel;

//use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use Excel;

use App\Models\Admin\User\Users;

class ExcelController extends Controller

{

/**

* 导出

*

* @param Users $users

* @return mixed

*/

public function excelExport(Request $request,Users $users)

{

//筛选条件 -- 根据需要 -- 修改你的查询语句

//$where['client_id'] = $request->client_id;

$data = $users->get()->toArray();

return Excel::create('用户数据导出', function($excel) use ($data) {

$excel->sheet('用户数据导出', function($sheet) use ($data)

{

$sheet->cell('A1', function($cell) {$cell->setValue('用户名'); });

$sheet->cell('B1', function($cell) {$cell->setValue('角色名'); });

$sheet->cell('C1', function($cell) {$cell->setValue('登陆IP'); });

$sheet->cell('D1', function($cell) {$cell->setValue('登陆时间'); });

if (!empty($data)) {

foreach ($data as $key => $value) {

$i= $key+2;

$sheet->cell('A'.$i, $value['username']);

$sheet->cell('B'.$i, $value['name']);

$sheet->cell('C'.$i, $value['login_ip']);

$sheet->cell('D'.$i, $value['login_at']);

}

}

});

})->download('xlsx');

}

}

5、测试看看能不能成功:

6683

导出就到此结束啦!希望能帮到你!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值