1. 程式人生 > >使用easypoi或者easyexcel多表頭匯出

使用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<ReportDemandExportVO> demandList; @ExcelCollection(name = "站址篩查",orderNum = "4") private List<ReportSiteExportVO> siteList; @ExcelCollection(name = "訂單階段",orderNum = "4") private List<ReportOrderExportVO> orderList; @ExcelCollection(name = "交付階段",orderNum = "4")
private List<ReportHandOverExportVO> handOverList; public List<ReportDemandExportVO> getDemandList() { return demandList; } public void setDemandList(List<ReportDemandExportVO> demandList) { this.demandList = demandList; } public List<ReportSiteExportVO> getSiteList() { return siteList; } public void setSiteList(List<ReportSiteExportVO> siteList) { this.siteList = siteList; } public List<ReportOrderExportVO> getOrderList() { return orderList; } public void setOrderList(List<ReportOrderExportVO> orderList) { this.orderList = orderList; } public List<ReportHandOverExportVO> getHandOverList() { return handOverList; } public void setHandOverList(List<ReportHandOverExportVO> 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<ReportIndexStateDetialVO> list = reportFormsService.getReportBatchIndexStateDetailStatistics(province,batchId);//獲取要匯出的資料
    List<SysProvinceVO> provinceVOs = reportFormsService.getOneProvince(province);
    List<ReportIndexStateDetailExportVO> exportList = new ArrayList<>();
    for (int i = 0; i<list.size() ; i++){//這個for迴圈是把獲取到的資料轉換成要匯出的那個實體類裡面。
        ReportIndexStateDetailExportVO exportVO = new ReportIndexStateDetailExportVO();
        exportVO.setRegion(list.get(i).getRegName());
        exportVO.setBatchName(list.get(i).getBatchName());
        List<ReportDemandExportVO> 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<ReportSiteExportVO> 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<ReportOrderExportVO> 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<ReportHandOverExportVO> 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來實現。就感覺沒有必要。

最終的效果如下圖:

2、easyexcel實現多表頭匯出。

       是不是覺得第一種使用easypoi的@ExcelCollection來實現匯出是一件非常麻煩的事情,為了一個多表頭匯出要多寫很多無用的程式碼。下面來介紹一下easyexcel的多表頭匯出,很簡單的。只需要一個module就ok了。

controller按照上面的寫就可以了,這樣是不是就簡單很多了。。。