因项目需要,导出如图一的格式,想了各种方法。终于找到了一种比较简单的方式。李利用空余时间整理整理,留念一下。
引入jar包
cn.afterturn
easypoi-base
3.1.0
cn.afterturn
easypoi-web
3.1.0
cn.afterturn
easypoi-annotation
3.1.0
一、首先创建一个订单实体类。(OrderBean)
@Data
public class OrderBean {
@Excel(name = "订单编号_order",needMerge = true)
private String truckCode;
@Excel(name = "下单时间",needMerge = true,format = "yyyy-MM-dd HH:mm:ss")
private Date orderDate;
@Excel(name = "下单备注",needMerge = true)
private String orderRemark;
}
二、订单明细的实体类。(OrderDetailBean)
@Data
public class OrderDetailBean {
@Excel(name = "商品名称")
private String goodsName;
@Excel(name = "数量")
private Integer goodsNumber;
@Excel(name = "商品单价")
private BigDecimal unitPrice;
@Excel(name = "金额",isStatistics=true)
private BigDecimal detailPrice;
@Excel(name = "备注")
private String detailRemark;
}
三、把两个实体类整合起来
@Data
@ExcelTarget("totalBean")
public class TotalBean {
@ExcelEntity(id="order")
private OrderBean orderBean;
@ExcelCollection(name = "订单明细",orderNum="5")
private ListdetailBeans;
}
四、
你想导出的数据为recoderList;
声明一个 List list=new ArrayList<>();
把recoderList的属性值分别赋值给list集合里面的属性值。
五、最后调用ExcelUtils的方法。就可以导出成功。
public void export(HttpServletResponse response) throws Exception {
ExcelUtils.exportExcel(response, "订单统计", list, TotalBean.class);
}
转载需注明:https://blog.csdn.net/juan1997/article/details/90679305