easypoi导出一对多复杂导出

导入依赖:

<!--easypoi 导入导出 -->
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-base</artifactId>
    <version>4.2.0</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-web</artifactId>
    <version>4.2.0</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-annotation</artifactId>
    <version>4.2.0</version>
</dependency>

主表

package com.diye.cloud.project.modular.competition.vo;

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelCollection;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 导出送出培训信息接收类(主表)
 * 2022年7月26日09:16:43
 * 张小辉
 */

@Data
public class CmpInfoExportVo {


    private String id;

    @Excel(name="培训性质",needMerge = true,replace = {"计划内_0","计划外_1"},width = 20)
    private String planProperties;

    @Excel(name="类别",needMerge = true,replace = {"国网_0","省专业部门_1","系统外_2"},width = 20)
    private String projectType;

    @Excel(name="培训班名称",needMerge = true,width = 30)
    private String className;

    @Excel(name="开始时间" , format = "yyyy-MM-dd",needMerge = true,width = 20)
    private Date planStartTime;

    @Excel(name="结束时间" , format = "yyyy-MM-dd",needMerge = true,width = 20)
    private Date planEndTime;


    @Excel(name="培训地点",needMerge = true,width = 20)
    private String address;

    @Excel(name="培训天数",needMerge = true,width = 20)
    private Integer planDays;

    @Excel(name="费用标准",needMerge = true,width = 20)
    private BigDecimal bidPrice;

    @Excel(name="总人数",needMerge = true,width = 20)
    private Integer peopleNum;

    @ExcelCollection(name = "")
    private List<StudentInfoExportVo> students;

}

子表

package com.diye.cloud.project.modular.competition.vo;


import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Data;

/**
 * 导出送出培训-学生信息接收类(子表)
 * 2022年7月26日09:24:20
 * 张小辉
 */
@Data
public class StudentInfoExportVo {
    @Excel(name="参培单位",width = 20)
    private String companyName;
    @Excel(name="参培部门",width = 20)
    private String deptName;
    @Excel(name="姓名",width = 20)
    private String name;
    @Excel(name="学历",width = 20)
    private String education;
    @Excel(name="职务",width = 20)
    private String post;
    @Excel(name="岗位",width = 20)
    private String station;
    @Excel(name="技术等级",width = 20)
    private String technicalLevel;
    @Excel(name="技能等级",width = 20)
    private String skillLevel;

}

controller


```java
 @SneakyThrows
    @PostResource(name = "根据条件查询导出", path = "/exportExcel", requiredPermission = false)
    @ApiOperation(value = "根据条件查询导出")
    public void exportExcel(@RequestBody CmpParam param,
                            HttpServletResponse response) {
        param = buildCmpParam(param);
        List<CmpInfoExportVo> exportList = cmpService.getExportList(param);
        List<StudentInfoExportVo> studentList = new ArrayList<>();
        for (CmpInfoExportVo cmpInfoExportVo : exportList) {
            //根据id去查询学员
            studentList = cmpStudentMaterialsService.getExportList(cmpInfoExportVo.getId());
            cmpInfoExportVo.setStudents(studentList);
            Integer peopleNum = cmpService.getPeopleNumber(cmpInfoExportVo.getId());
            if(ToolUtil.isNotEmpty(peopleNum)){
                cmpInfoExportVo.setPeopleNum(peopleNum);
            }else{
                cmpInfoExportVo.setPeopleNum(0);
            }
        }
        // 简单模板导出方法
        ExportParams params = new ExportParams();
        params.setSheetName("送出培训");//设置sheet名
        Workbook workbook = ExcelExportUtil.exportExcel(params, CmpInfoExportVo.class, exportList);
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" +
                    URLEncoder.encode("送出培训导出", "UTF-8"));
            OutputStream ouputStream = response.getOutputStream();
            workbook.write(ouputStream);
            ouputStream.flush();
            ouputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


![在这里插入图片描述](https://img-blog.csdnimg.cn/04ac9dd2eb154c1ba061afece79378a5.png)
同时可参考easypoi一对多复杂导入:https://blog.csdn.net/qq_26144365/article/details/126947292



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在使用 EasyPoi 导出一对多数据时,需要使用 `@ExcelCollection` 注解来标识一对多的关系。下面是一个示例: 假设我们有两个实体类: `Order` 和 `OrderItem`,一个订单可以对应多个订单项。 ```java public class Order { @Excel(name = "订单编号", orderNum = "0") private String orderId; @Excel(name = "订单总价", orderNum = "1") private BigDecimal totalPrice; @ExcelCollection(name = "订单项", orderNum = "2") private List<OrderItem> orderItemList; // 省略 getter 和 setter 方法 } public class OrderItem { @Excel(name = "订单项编号", orderNum = "0") private String orderItemId; @Excel(name = "商品名称", orderNum = "1") private String productName; @Excel(name = "商品数量", orderNum = "2") private Integer productQuantity; // 省略 getter 和 setter 方法 } ``` 在 `Order` 类中,我们使用 `@ExcelCollection` 注解标识 `orderItemList` 属性,表示一个订单有多个订单项。在导出时,EasyPoi 会自动处理一对多的数据,并将订单项列表展开成多行数据。 下面是一个导出示例: ```java // 构造数据 List<Order> orderList = new ArrayList<>(); Order order1 = new Order(); order1.setOrderId("1001"); order1.setTotalPrice(new BigDecimal(100)); List<OrderItem> orderItemList1 = new ArrayList<>(); OrderItem item1 = new OrderItem(); item1.setOrderItemId("101"); item1.setProductName("商品1"); item1.setProductQuantity(2); orderItemList1.add(item1); OrderItem item2 = new OrderItem(); item2.setOrderItemId("102"); item2.setProductName("商品2"); item2.setProductQuantity(3); orderItemList1.add(item2); order1.setOrderItemList(orderItemList1); orderList.add(order1); // 导出 ExcelExportUtil.exportExcel(new ExportParams("订单列表", "订单"), Order.class, orderList, new FileOutputStream("order.xls")); ``` 在导出结果中,我们可以看到订单项被展开成了多行数据: | 订单编号 | 订单总价 | 订单项编号 | 商品名称 | 商品数量 | | :-----: | :-----: | :-----: | :-----: | :-----: | | 1001 | 100 | 101 | 商品1 | 2 | | | | 102 | 商品2 | 3 |

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值