EasyPoi实现一对多导出(亲测有效)

  1. 首先引入依赖
        <!--poi-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.1.3</version>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
            <optional>true</optional>
        </dependency>

 2. 创建实体类

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelCollection;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
import java.util.List;

/**
 * @author : xx
 * @description :
 * @createDate : 2024/11/12 10:15
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
    private String id;

    @Excel(name = "员工姓名", width = 30,needMerge = true)
    private String name;

    @Excel(name = "员工性别",replace ={"男生_1","女生_2"},needMerge = true)
    private int sex;

    @Excel(name = "开始日期",format = "yyyy-MM-dd", width = 20,needMerge = true)
    private Date birthday;

    @ExcelCollection(name = "报销金额")
    private List<BaoXiao> baoxiao;
}
import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author : xx
 * @description :
 * @createDate : 2024/11/12 10:28
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BaoXiao {
    @Excel(name = "火车票金额",width = 30)
    private String hcpje;

    @Excel(name = "汽车票金额",width = 30)
    private String qcpjw;
}

3. Controller层

@GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        List<Employee> data = getData();
        try{
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("差旅报销金额", "utf-8");
            response.setHeader("Content-Disposition", "attachment; filename="+fileName+".xlsx");
            ExcelExportUtil.exportExcel(new ExportParams(),Employee.class,data).write(response.getOutputStream());
        }catch (IOException e){
            e.printStackTrace();
        }

    }


    /*
    * 模拟数据
    * */
    private List<Employee> getData(){
        List<BaoXiao> xiaos = new ArrayList<>();
        List<BaoXiao> xiaos2 = new ArrayList<>();
        BaoXiao baoXiao1 = new BaoXiao("100","200");
        BaoXiao baoXiao2 = new BaoXiao("110","210");
        BaoXiao baoXiao3 = new BaoXiao("120","220");
        BaoXiao baoXiao4 = new BaoXiao("130","230");
        BaoXiao baoXiao5 = new BaoXiao("140","240");
        xiaos.add(baoXiao1);
        xiaos.add(baoXiao2);
        xiaos.add(baoXiao3);
        xiaos2.add(baoXiao4);
        xiaos2.add(baoXiao5);

        List<Employee> list = new ArrayList<>();
        Employee s1 = new Employee("001","张三",1,new Date(),xiaos);
        Employee s2 = new Employee("002","李四",2,new Date(),xiaos2);
        list.add(s1);
        list.add(s2);
        return list;
    }

导出后的结果如下图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱码仕.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值