Laravel Excel 怎么根据参数导出自定义列

nova 版本的有看到,docs.laravel-excel.com/nova/1.x/ex...

但是我不是 nova 版本的,普通的。

我现在的代码是

<?php

namespace App\Exports;

use App\Models\MedicineType;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;

class MedicineTypeExport implements FromCollection, WithMapping, WithHeadings, ShouldAutoSize
{
    use Exportable;

    public function collection()
    {
        return MedicineType::listExport();
    }

    public function map($row): array
    {
        return [
            $row->name ?? '',
            $row->legal_name ?? '',
            $row->en_name ?? '',
            $row->ucode_b ?? '',
            $row->category ?? '',
        ];
    }

    public function headings(): array
    {
        return [
            '药品名称',
            '药品法定名称',
            '药品英文名称',
            '药品唯一标识符',
            '所属药品品种',
        ];
    }
}
(new MedicineTypeExport)->download('药品管理-药品大类导出.xlsx');

需要

比如,导出时,我只需要传入 name、en_name,下载的 Excel 中只有显示这两列。

无论在现实或是网络中,我都是孤独的。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
最佳答案

我之前是把逻辑写到 __construct() 中了,然后额外定义了 $headings 和 数据的变量,

class SurveyUserReportExport implements FromArray, WithHeadings, WithStyles, Responsable
{
    use Exportable;
    /**
    * 导出数据
    * @var array
    */
    private $data_array = [];
    /**
    * 标题
    * @var string[]
     */
    private $headings = [];

    public function __construct($param){
        ...
    }

    public function headings(): array
    {
        return $this->headings;
    }
    public function array(): array
    {
        return $this->data_array;
    }
2年前 评论
小李世界 (楼主) 2年前
讨论数量: 4
laradocs

那你现在点下载是什么结果呢?

2年前 评论
小李世界 (楼主) 2年前

我之前是把逻辑写到 __construct() 中了,然后额外定义了 $headings 和 数据的变量,

class SurveyUserReportExport implements FromArray, WithHeadings, WithStyles, Responsable
{
    use Exportable;
    /**
    * 导出数据
    * @var array
    */
    private $data_array = [];
    /**
    * 标题
    * @var string[]
     */
    private $headings = [];

    public function __construct($param){
        ...
    }

    public function headings(): array
    {
        return $this->headings;
    }
    public function array(): array
    {
        return $this->data_array;
    }
2年前 评论
小李世界 (楼主) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!