lumen框架通过maatwebsite/excel 3.1 实现简单导出

lumen框架通过maatwebsite/excel 3.1 实现简单导出

文章来源:https://www.cnblogs.com/smilevv/p/13518588.html 侵权联系马上删除

1.安装 maatwebsite/excel 成功的话如图

composer require maatwebsite/excel

image-20240111153703566

  1. 在bootstrap/app.php中加入
$app->register(Maatwebsite\Excel\ExcelServiceProvider::class);

3.在app目录下创建导出类的目录Exports -> app/Exports

image-20240112091432255

4.在Exports目录中创建 InvoicesExport.php文件

<?php
namespace App\Exports;

use App\Invoice;
use Maatwebsite\Excel\Concerns\FromArray;

// excel操作导出
class InvoicesExport implements FromArray
{
    protected $invoices;
    public function __construct(array $invoices)
    {
        $this->invoices = $invoices;
    }

    public function array(): array
    {
        return $this->invoices;
    }
}

5.在控制器中使用

	// 使用示例
	public function export_user_info(Request $request)
    {
        $rows = User::get()->toArray();		// rows这个就是你想要导出的数据

        // 添加表头
        array_unshift($rows, ['工号', '姓名', '性别']);
        
        $export = new InvoicesExport($rows);
        $bool = Excel::download($export, '人员导出.xlsx');
        return $bool;		// 注:返回的是文件流,需要前端进行解析
    }

‘人员导出.xlsx’);
return $bool; // 注:返回的是文件流,需要前端进行解析
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值