laravel Maatwebsite3.1

<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Events\AfterSheet;

class UserExport implements FromCollection, WithHeadings, WithEvents
{
use Exportable;
 
private $data;
private $headings;

//数据注入
public function __construct($data, $headings)
{
$this->data = $data;
$this->headings = $headings;
}

//实现FromCollection接口
public function collection()
{
return collect($this->data);
}

//实现WithHeadings接口
public function headings(): array
{
return $this->headings;
}

public function registerEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $sheet) {
$sheet->getDelegate()->getColumnDimension('A')->setWidth(20);
$sheet->getDelegate()->getColumnDimension('B')->setWidth(20);
$sheet->getDelegate()->getColumnDimension('C')->setWidth(20);
$sheet->getDelegate()->getColumnDimension('D')->setWidth(20);
$sheet->getDelegate()->getColumnDimension('E')->setWidth(20);
$sheet->getDelegate()->getColumnDimension('F')->setWidth(20);
$sheet->getDelegate()->getColumnDimension('G')->setWidth(20);
$sheet->getDelegate()->getColumnDimension('H')->setWidth(20);
$sheet->getDelegate()->getColumnDimension('I')->setWidth(20);
}
];
}
}
 
//控制器
<?php

namespace App\Http\Controllers\Admin;

use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\UserExport;

class UserController extends Controller
{
public function __construct(User $model)
{
$this->model = $model;
}
 
public function export(Request $request)
{
$search = $request->only('k2', 'keys');
if (!empty($search)) {
if (isset($search['k2']) && $search['k2'] != null) {
$search_time = explode(' - ', $search['k2']);
$this->model = $this->model->whereBetween('created_at', [strtotime($search_time[0]), strtotime($search_time[1])]);
}
if (isset($search['keys']) && $search['keys'] != null) {
$this->model = $this->model->where('uid', 'like', '%' . $search['keys'] . '%')->orWhere('name', 'like', '%' . $search['keys'] . '%')->orWhere('mobile', 'like', '%' . $search['keys'] . '%');
}
}
$data = $this->model->with('userLevel:id,title')->get(['uid', 'name', 'mobile', 'pid', 'level_id', 'shop_money', 'integral', 'commission', 'created_at']);

$array = [];

if ($data->isNotEmpty()) {
foreach ($data as $key => $value) {
$array[$key] = [
'uid' => $value->uid,
'pid' => $value->pid == 0 ? '无推荐人' : User::where('id', $value->pid)->value('uid'),
'name' => $value->name,
'mobile' => $value->mobile,
'level_id' => $value->userLevel->title,
'shop_money' => $value->shop_money,
'integral' => $value->integral,
'commission' => $value->commission,
'created_at' => $value->created_at,
];
}
}
$headings = [
'账号',
'推荐人',
'姓名',
'电话',
'等级',
'余额',
'积分',
'佣金',
'注册',
];

return Excel::download(new UserExport($array, $headings), 'users.xlsx');
}
}

转载于:https://www.cnblogs.com/ssx314/p/11350649.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值