laravel使用excel3.0导出

博客介绍了Laravel Excel扩展包相关内容。给出官方文档链接,安装要求php7.0以上,可在根目录用命令composer require maatwebsite/excel ~3.0安装扩展包,并在app.php文件添加语句。还说明了快速入门步骤,包括创建自定义导出类、按需更改及进行导出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官方文档: https://docs.laravel-excel.com/3.0/exports/mapping.html
安装:
要求:php7.0以上 。。。
①在根目录中使用命令:composer require maatwebsite/excel ~3.0安装扩展包
②在app.php文件中添加以下语句:

'providers' => [
 .....
 Maatwebsite\Excel\ExcelServiceProvider::class,
],
'aliases' => [
.....
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]

快速入门:
1.创建一个自定义导出的类UsersExport
2.根据需求做出如下更改

<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 2019/6/18
* Time: 11:41
*/
namespace App\Http\Explore;
use App\Http\Model\Users;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;


class UsersExport implements FromCollection,WithMapping,WithHeadings
{
   public function collection()
   {
       $result=Users::get();
       foreach ($result as $v){
           $v['create_time']=date('Y-m-d H:i:s',$v['create_time']);
       }
    return $result;
   }


    /**数据映射
     * @param mixed $invoice
     * @return array
     */
   public function map($invoice): array
   {
       return [
            $invoice->username,
            $invoice->create_time,
           //对于数字0以字符串形式输出或者使用WithStrictNullComparison接口
           "$invoice->status",
       ];
   }


    /**定义表单头
     * @return array
     */
    public function headings(): array
   {
       return [
          '名字',
          '创建时间',
          '状态',
       ];
   }
}

3.进行导出

/**
* 导出测试
*/
public function exploreTest(Request $request)
{
    return Excel::download(new UsersExport(), date('YmdHis').'测试.xlsx');
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值