使用easypoi或者easyexcel多表头导出

1、使用easypoi实现多表头导出。

其实使用easypoi实现单表头的导出很简单,百度一下到处都是。但是对于多表头的导出搜了很久都没有搜到,网上大多都是自己写自己封装的,没有使用easypoi注解更简单的实现。所以这里记录一下使用easypoi的 @ExcelCollection 注解来实现多表头的导出。

首先要导入easypoi相关的jar包。

导出主module:ReportIndexStateDetailExportVO

/**
 * Created by author on 2018/8/13.
 * 批次指标各阶段数据统计导出VO
 */
public class ReportIndexStateDetailExportVO implements Serializable {

    private static final long serialVersionUID = 1L;

    @Excel(isWrap = false, name = "地市名称",orderNum = "1",width = 30,needMerge = true)
    private String region;

    @Excel(isWrap = false, name = "所属批次",orderNum = "2",width = 30,needMerge = true)
    private String batchName;

    @ExcelCollection(name = "需求阶段",orderNum = "2")
    private List demandList;

    @ExcelCollection(name = "站址筛查",orderNum = "4")
    private List siteList;

    @ExcelCollection(name = "订单阶段",orderNum = "4")
    private List orderList;

    @ExcelCollection(name = "交付阶段",orderNum = "4")
    private List handOverList;

    public List getDemandList() {
        return demandList;
    }

    public void setDemandList(List demandList) {
        this.demandList = demandList;
    }

    public List getSiteList() {
        return siteList;
    }

    public void setSiteList(List siteList) {
        this.siteList = siteList;
    }

    public List getOrderList() {
        return orderList;
    }

    public void setOrderList(List orderList) {
        this.orderList = orderList;
    }

    public List getHandOverList() {
        return handOverList;
    }

    public void setHandOverList(List handOverList) {
        this.handOverList = handOverList;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public String getBatchName() {
        return batchName;
    }

    public void setBatchName(String batchName) {
        this.batchName = batchName;
    }
}

主module想当于就是制定导出的一个大的框架,里面还有4个List,这4个list里面要实现的就是要多表头导出的数据。

4个实体的编写:

public class ReportDemandExportVO implements Serializable {

    private static final long serialVersionUID = 1L;

    @Excel(isWrap = false, name = "新址新建",orderNum = "1",width = 15)
    private Integer demandNewAddressNum;//需求阶段新址新建数量

    @Excel(isWrap = false, name = "共址新建",orderNum = "2",width = 15)
    private Integer demandCoAddressNum;//需求阶段共址新建数量

    public Integer getDemandNewAddressNum() {
        return demandNewAddressNum;
    }

    public void setDemandNewAddressNum(Integer demandNewAddressNum) {
        this.demandNewAddressNum = demandNewAddressNum;
    }

    public Integer getDemandCoAddressNum() {
        return demandCoAddressNum;
    }

    public void setDemandCoAddressNum(Integer demandCoAddressNum) {
        this.demandCoAddressNum = demandCoAddressNum;
    }
}

 

public class ReportSiteExportVO implements Serializable {

    private static final long serialVersionUID = 1L;

    @Excel(isWrap = false, name = "新址新建独享站数量",orderNum = "1",width = 30)
    private Integer siteNewAddressExclusiveNum;//站址筛查阶段新址新建独享数量

    @Excel(isWrap = false, name = "新址新建共享站数量",orderNum = "2",width = 30)
    private Integer siteNewAddressSharedNum;//站址筛查阶段新址新建共享数量

    @Excel(isWrap = false, name = "共址新建数量",orderNum = "3",width = 15)
    private Integer siteCoAddressNum;//站址筛查阶段共址新建数量

    @Excel(isWrap = false, name = "当前共享率",orderNum = "4",suffix = "%",width = 15)
    private String siteShareRateCurr;//站址筛查阶段共享率

    public Integer getSiteNewAddressExclusiveNum() {
        return siteNewAddressExclusiveNum;
    }

    public void setSiteNewAddressExclusiveNum(Integer siteNewAddressExclusiveNum) {
        this.siteNewAddressExclusiveNum = siteNewAddressExclusiveNum;
    }

    public Integer getSiteNewAddressSharedNum() {
        return siteNewAddressSharedNum;
    }

    public void setSiteNewAddressSharedNum(Integer siteNewAddressSharedNum) {
        this.siteNewAddressSharedNum = siteNewAddressSharedNum;
    }

    public Integer getSiteCoAddressNum() {
        return siteCoAddressNum;
    }

    public void setSiteCoAddressNum(Integer siteCoAddressNum) {
        this.siteCoAddressNum = siteCoAddressNum;
    }

    public String getSiteShareRateCurr() {
        return siteShareRateCurr;
    }

    public void setSiteShareRateCurr(String siteShareRateCurr) {
        this.siteShareRateCurr = siteShareRateCurr;
    }
}

 

public class ReportOrderExportVO implements Serializable {

    private static final long serialVersionUID = 1L;

    @Excel(isWrap = false, name = "新址新建独享站数量",orderNum = "1",width = 30)
    private Integer orderNewAddressExclusiveNum;//订单阶段新址新建独享数量

    @Excel(isWrap = false, name = "新址新建共享站数量",orderNum = "2",width = 30)
    private Integer orderNewAddressSharedNum;//订单阶段新址新建共享数量

    @Excel(isWrap = false, name = "共址新建数量",orderNum = "3",width = 15)
    private Integer orderCoAddressNum;//订单阶段共址新建数量

    @Excel(isWrap = false, name = "当前共享率",orderNum = "4",suffix = "%",width = 15)
    private String orderShareRateCurr;//订单阶段共享率

    public Integer getOrderNewAddressExclusiveNum() {
        return orderNewAddressExclusiveNum;
    }

    public void setOrderNewAddressExclusiveNum(Integer orderNewAddressExclusiveNum) {
        this.orderNewAddressExclusiveNum = orderNewAddressExclusiveNum;
    }

    public Integer getOrderNewAddressSharedNum() {
        return orderNewAddressSharedNum;
    }

    public void setOrderNewAddressSharedNum(Integer orderNewAddressSharedNum) {
        this.orderNewAddressSharedNum = orderNewAddressSharedNum;
    }

    public Integer getOrderCoAddressNum() {
        return orderCoAddressNum;
    }

    public void setOrderCoAddressNum(Integer orderCoAddressNum) {
        this.orderCoAddressNum = orderCoAddressNum;
    }

    public String getOrderShareRateCurr() {
        return orderShareRateCurr;
    }

    public void setOrderShareRateCurr(String orderShareRateCurr) {
        this.orderShareRateCurr = orderShareRateCurr;
    }
}

 

public class ReportHandOverExportVO implements Serializable {

    private static final long serialVersionUID = 1L;

    @Excel(isWrap = false, name = "新址新建独享站数量",orderNum = "1",width = 30)
    private Integer handoverNewAddressExclusiveNum;//交付阶段新址新建独享数量

    @Excel(isWrap = false, name = "新址新建共享站数量",orderNum = "2",width = 30)
    private Integer handoverNewAddressSharedNum;//交付阶段新址新建共享数量

    @Excel(isWrap = false, name = "共址新建数量",orderNum = "3",width = 15)
    private Integer handoverCoAddressNum;//交付阶段共址新建数量

    @Excel(isWrap = false, name = "当前共享率",orderNum = "4",suffix = "%",width = 15)
    private String handoverShareRateCurr;//交付阶段共享率

    public Integer getHandoverNewAddressExclusiveNum() {
        return handoverNewAddressExclusiveNum;
    }

    public void setHandoverNewAddressExclusiveNum(Integer handoverNewAddressExclusiveNum) {
        this.handoverNewAddressExclusiveNum = handoverNewAddressExclusiveNum;
    }

    public Integer getHandoverNewAddressSharedNum() {
        return handoverNewAddressSharedNum;
    }

    public void setHandoverNewAddressSharedNum(Integer handoverNewAddressSharedNum) {
        this.handoverNewAddressSharedNum = handoverNewAddressSharedNum;
    }

    public Integer getHandoverCoAddressNum() {
        return handoverCoAddressNum;
    }

    public void setHandoverCoAddressNum(Integer handoverCoAddressNum) {
        this.handoverCoAddressNum = handoverCoAddressNum;
    }

    public String getHandoverShareRateCurr() {
        return handoverShareRateCurr;
    }

    public void setHandoverShareRateCurr(String handoverShareRateCurr) {
        this.handoverShareRateCurr = handoverShareRateCurr;
    }
}

 

controller的编写:

/**
 * 导出 批次指标各阶段数据统计
 * */
@RequestMapping(value = "/exportIndexBatchDetailResultConfirm", method = RequestMethod.GET)
public void exportIndexBatchDetailResultConfirm(HttpServletRequest request, HttpServletResponse response,String province,String batchId) {
    // 数据权限过滤(根据登陆用户过滤)
    UserLoginInfo loginInfo = (UserLoginInfo) request.getSession().getAttribute("user");
    if (loginInfo == null) {
        throw new BusinessException(PromptMessageComm.LOGIN_FAILED);
    }
    List list = reportFormsService.getReportBatchIndexStateDetailStatistics(province,batchId);//获取要导出的数据
    List provinceVOs = reportFormsService.getOneProvince(province);
    List exportList = new ArrayList<>();
    for (int i = 0; i//这个for循环是把获取到的数据转换成要导出的那个实体类里面。
        ReportIndexStateDetailExportVO exportVO = new ReportIndexStateDetailExportVO();
        exportVO.setRegion(list.get(i).getRegName());
        exportVO.setBatchName(list.get(i).getBatchName());
        List demandExportVOs = new ArrayList<>();
        ReportDemandExportVO demandExportVO = new ReportDemandExportVO();
        demandExportVO.setDemandNewAddressNum(list.get(i).getDemandNewAddressNum());
        demandExportVO.setDemandCoAddressNum(list.get(i).getDemandCoAddressNum());
        demandExportVOs.add(demandExportVO);
        exportVO.setDemandList(demandExportVOs);

        List siteExportVOs = new ArrayList<>();
        ReportSiteExportVO siteExportVO = new ReportSiteExportVO();
        siteExportVO.setSiteNewAddressExclusiveNum(list.get(i).getSiteNewAddressExclusiveNum());
        siteExportVO.setSiteNewAddressSharedNum(list.get(i).getSiteNewAddressSharedNum());
        siteExportVO.setSiteCoAddressNum(list.get(i).getSiteCoAddressNum());
        siteExportVO.setSiteShareRateCurr(list.get(i).getSiteShareRateCurr());
        siteExportVOs.add(siteExportVO);
        exportVO.setSiteList(siteExportVOs);

        List orderExportVOs = new ArrayList<>();
        ReportOrderExportVO orderExportVO = new ReportOrderExportVO();
        orderExportVO.setOrderCoAddressNum(list.get(i).getOrderCoAddressNum());
        orderExportVO.setOrderNewAddressExclusiveNum(list.get(i).getOrderNewAddressExclusiveNum());
        orderExportVO.setOrderNewAddressSharedNum(list.get(i).getOrderNewAddressSharedNum());
        orderExportVO.setOrderShareRateCurr(list.get(i).getOrderShareRateCurr());
        orderExportVOs.add(orderExportVO);
        exportVO.setOrderList(orderExportVOs);

        List handOverExportVOs = new ArrayList<>();
        ReportHandOverExportVO handOverExportVO = new ReportHandOverExportVO();
        handOverExportVO.setHandoverCoAddressNum(list.get(i).getHandoverCoAddressNum());
        handOverExportVO.setHandoverShareRateCurr(list.get(i).getHandoverShareRateCurr());
        handOverExportVO.setHandoverNewAddressExclusiveNum(list.get(i).getHandoverNewAddressExclusiveNum());
        handOverExportVO.setHandoverNewAddressSharedNum(list.get(i).getHandoverNewAddressSharedNum());
        handOverExportVOs.add(handOverExportVO);
        exportVO.setHandOverList(handOverExportVOs);

        exportList.add(exportVO);
    }
    // 设置导出参数
    Workbook workBook = ExcelExportUtil.exportExcel(new ExportParams(), ReportIndexStateDetailExportVO.class, exportList);
    workBook.setSheetName(0, provinceVOs.get(0).getPrvName()+"批次指标各阶段数据明细");
    FileUtils.downFile(workBook, provinceVOs.get(0).getPrvName()+"批次指标各阶段数据明细.XLS", request, response);
}

导出js方法:

//导出详细列表
function exportDetailData(){
   var province = '';
   if ( localStorage.getItem("prvCode") === 'HQ' ) {
      name.field = 'prvSname';
      province = "000000";
   }
   var params = {
      batchId: document.getElementById('batch').value,
      province: province || getCodeByname(localStorage.getItem('prvCode'))
   };
   window.open("exportIndexBatchDetailResultConfirm?" + $.param(params), "_blank");
}

这种方法导出多表头excel还是比较麻烦的,因为我要导出多个表头就要建立相对应多的实体,使用@ExcelCollection来实现。就感觉没有必要。

最终的效果如下图:

使用easypoi或者easyexcel多表头导出_第1张图片

使用easypoi或者easyexcel多表头导出_第2张图片

2、easyexcel实现多表头导出。

       是不是觉得第一种使用easypoi的@ExcelCollection来实现导出是一件非常麻烦的事情,为了一个多表头导出要多写很多无用的代码。下面来介绍一下easyexcel的多表头导出,很简单的。只需要一个module就ok了。

使用easypoi或者easyexcel多表头导出_第3张图片

使用easypoi或者easyexcel多表头导出_第4张图片

controller按照上面的写就可以了,这样是不是就简单很多了。。。

你可能感兴趣的:(java)