java使用easypoi导出多表头文件
效果:
导入依赖:dependencygroupIdcn。afterturngroupIdeasypoibaseartifactIdversion3。2。0versiondependencydependencygroupIdcn。afterturngroupIdeasypoiwebartifactIdversion3。2。0versiondependencydependencygroupIdcn。afterturngroupIdeasypoiannotationartifactIdversion3。2。0versiondependency
创建Excel对应文件CostReductionStatiImprotVoDataEqualsAndHashCode(callSuperfalse)publicclassCostReductionStatiImprotVoimplementsSerializable{privatestaticfinallongserialVersionUID1L;年度Excel(isWrapfalse,name年度,orderNum1,width20,needMergetrue)privateStringyear;供应商编码Excel(isWrapfalse,name供应商编码,orderNum2,width23,needMergetrue)privateStringsuppCode;供应商名称Excel(isWrapfalse,name供应商名称,orderNum3,width30,needMergetrue)privateStringsuppName;物料编码Excel(isWrapfalse,name物料编码,orderNum4,width25,needMergetrue)privateStringmasterCode;物料名称Excel(isWrapfalse,name物料名称,orderNum5,width30,needMergetrue)privateStringmasterName;调价原因Excel(isWrapfalse,name调价原因,orderNum6,width30,needMergetrue)privateStringmodifyPriceReson;开始日期Excel(isWrapfalse,name开始日期,orderNum7,width30,needMergetrue)privateStringstartTime;单价差额Excel(isWrapfalse,name单价差额,orderNum8,width30,needMergetrue)privateStringunitPriceDifference;一月ExcelCollection(name一月,orderNum9)privateListJanMonthlyVojanuary;二月ExcelCollection(name二月,orderNum10)privateListFebMonthlyVofebruary;三月ExcelCollection(name三月,orderNum11)privateListMarMonthlyVomarch;四月ExcelCollection(name四月,orderNum12)privateListapril;五月ExcelCollection(name五月,orderNum13)privateListMayMonthlyVomay;六月ExcelCollection(name六月,orderNum14)privateListJunMonthlyVojune;七月ExcelCollection(name七月,orderNum15)privateListJulyMonthlyVojuly;八月ExcelCollection(name八月,orderNum16)privateListaugust;九月ExcelCollection(name九月,orderNum17)privateListSepMonthlyVoseptember;十月ExcelCollection(name十月,orderNum18)privateListOctMonthlyVooctober;十一月ExcelCollection(name十一月,orderNum19)privateListNovMonthlyVonovember;十二月ExcelCollection(name十二月,orderNum20)privateListDecMonthlyVodecember;累计用量合计Excel(isWrapfalse,name累计用量合计,orderNum21,width30,needMergetrue)privateLongcumulativeUsSum;降本金额合计Excel(isWrapfalse,name降本金额合计,orderNum22,width30,needMergetrue)privateStringcostReductAmountSum;统计时间Excel(isWrapfalse,name统计时间,orderNum23,width30,needMergetrue)privateStringstatiTime;}说明:orderNum:排序,如果属性顺序和表格字段顺序不一致,可已设置顺序width:表格列宽needMerge:是否需要合并单元格ExcelCollection(name十二月,orderNum20)对应双层表头字段特别注意!!!!踩坑属性字段必须遵守驼峰命名发,首字母小写!!!!!!注意属性,Excel支持属性类型不多,我使用的都是基本常用属性,在serviceImpl类中再做在转换
CostReductionStatiImprotVo类中,多层表头字段对应一个lislist里面的也需单独建VO
例如:JanMonthlyVoDataEqualsAndHashCode(callSuperfalse)publicclassJanMonthlyVoimplementsSerializable{privatestaticfinallongserialVersionUID2L;一月累计用量Excel(isWrapfalse,name一月累计用量,orderNum1,width15)privateIntegercumulativeUsJan;一月降本金额Excel(isWrapfalse,name一月降本金额,orderNum2,width15)privateStringcostReductAmountJan;}
Contorller层:导出多重表头paramresponsethrowsExceptionPostMapping(exportExcel)publicvoidexportExcel(HttpServletResponseresponse,RequestBodyPageParamVOparams)throwsException{buCostReductionStatiYWService。exportExcel(response,params);}
ServiceImpl层:导出paramresponsethrowsExceptionOverridepublicvoidexportExcel(HttpServletResponseresponse,PageParamVOparamVO)throwsException{ListCostReductionStatiImprotVoimportInfoListgetAllCostReductionImportInfo(paramVO);response。setContentType(applicationvnd。openxmlformatsofficedocument。spreadsheetml。sheet;charsetutf8);test。xls是弹出下载对话框的文件名,不能为中文,中文请自行编码response。setHeader(ContentDisposition,attachment;filenamefile。xlsx);HashMapString,ObjectmapnewHashMap();WorkbookworkbookExcelExportUtil。exportExcel(newExportParams(),CostReductionStatiImprotVo。class,importInfoList);workbook。setSheetName(0,降本统计);ServletOutputStreamoutputStreamresponse。getOutputStream();workbook。write(outputStream);outputStream。flush();outputStream。close();workbook。close();}获取数据publicListCostReductionStatiImprotVogetAllCostReductionImportInfo(PageParamVOparamVO){ArrayListCostReductionStatiImprotVoImprotVoListnewArrayList();ListDataFilterfilterparamVO。getFilter();ListBuCostReductionStatiEntityentityListbuCostReductionStatiMapper。selectList(WrapperFilter。buildQueryWrapper(newQueryWrapper(),filter));SimpleDateFormatformatnewSimpleDateFormat(yyyyMMddHH:ss:mm);if(CollectionUtils。isNotEmpty(entityList)){for(BuCostReductionStatiEntityentity:entityList){JanMonthlyVojanMonthlyVonewJanMonthlyVo();FebMonthlyVofebMonthlyVonewFebMonthlyVo();MarMonthlyVomarMonthlyVonewMarMonthlyVo();AprMonthlyVoaprMonthlyVonewAprMonthlyVo();MayMonthlyVomayMonthlyVonewMayMonthlyVo();JunMonthlyVojunMonthlyVonewJunMonthlyVo();JulyMonthlyVojulyMonthlyVonewJulyMonthlyVo();AugMonthlyVoaugMonthlyVonewAugMonthlyVo();SepMonthlyVosepMonthlyVonewSepMonthlyVo();OctMonthlyVooctMonthlyVonewOctMonthlyVo();NovMonthlyVonovMonthlyVonewNovMonthlyVo();DecMonthlyVodecMonthlyVonewDecMonthlyVo();ArrayListJanMonthlyVojanMonthlyVosnewArrayList();ArrayListFebMonthlyVofebMonthlyVosnewArrayList();ArrayListMarMonthlyVomarMonthlyVosnewArrayList();ArrayListaprMonthlyVosnewArrayList();ArrayListMayMonthlyVomayMonthlyVosnewArrayList();ArrayListJunMonthlyVojunMonthlyVosnewArrayList();ArrayListJulyMonthlyVojulyMonthlyVosnewArrayList();ArrayListaugMonthlyVosnewArrayList();ArrayListSepMonthlyVosepMonthlyVosnewArrayList();ArrayListOctMonthlyVooctMonthlyVosnewArrayList();ArrayListNovMonthlyVonovMonthlyVosnewArrayList();ArrayListDecMonthlyVodecMonthlyVosnewArrayList();CostReductionStatiImprotVoimprotVonewCostReductionStatiImprotVo();improtVo。setYear(entity。getYear());improtVo。setMasterCode(entity。getMasterCode());improtVo。setMasterName(entity。getMasterName());improtVo。setSuppCode(entity。getSuppCode());improtVo。setSuppName(entity。getSuppName());switch(entity。getModifyPriceReson()){caseT02:improtVo。setModifyPriceReson(BuCostReductionStatiConstans。T02CH);break;caseT03:improtVo。setModifyPriceReson(BuCostReductionStatiConstans。T03CH);break;caseT04:improtVo。setModifyPriceReson(BuCostReductionStatiConstans。T04CH);break;caseT05:improtVo。setModifyPriceReson(BuCostReductionStatiConstans。T05CH);break;caseT06:improtVo。setModifyPriceReson(BuCostReductionStatiConstans。T06CH);break;}improtVo。setStartTime(format。format(entity。getStartTime()));improtVo。setUnitPriceDifference(entity。getUnitPriceDifference()null?null:entity。getUnitPriceDifference()。toString());improtVo。setCumulativeUsSum(entity。getCumulativeUsSum());improtVo。setCostReductAmountSum(entity。getCostReductAmountSum()null?null:entity。getCostReductAmountSum()。toString());improtVo。setStatiTime(format。format(entity。getStatiTime()));janMonthlyVo。setCumulativeUsJan(entity。getCumulativeUsJan());janMonthlyVo。setCostReductAmountJan(entity。getCostReductAmountJan()null?null:entity。getCostReductAmountJan()。toString());janMonthlyVos。add(janMonthlyVo);improtVo。setJanuary(janMonthlyVos);febMonthlyVo。setCumulativeUsFeb(entity。getCumulativeUsFeb());febMonthlyVo。setCostReductAmountFeb(entity。getCostReductAmountFeb()null?null:entity。getCostReductAmountFeb()。toString());febMonthlyVos。add(febMonthlyVo);improtVo。setFebruary(febMonthlyVos);marMonthlyVo。setCumulativeUsMar(entity。getCumulativeUsMar());marMonthlyVo。setCostReductAmountMar(entity。getCostReductAmountMar()null?null:entity。getCostReductAmountMar()。toString());marMonthlyVos。add(marMonthlyVo);improtVo。setMarch(marMonthlyVos);aprMonthlyVo。setCumulativeUsApr(entity。getCumulativeUsApr());aprMonthlyVo。setCostReductAmountApr(entity。getCostReductAmountApr()null?null:entity。getCostReductAmountApr()。toString());aprMonthlyVos。add(aprMonthlyVo);improtVo。setApril(aprMonthlyVos);mayMonthlyVo。setCumulativeUsMay(entity。getCumulativeUsMay());mayMonthlyVo。setCostReductAmountMay(entity。getCostReductAmountMay()null?null:entity。getCostReductAmountMay()。toString());mayMonthlyVos。add(mayMonthlyVo);improtVo。setMay(mayMonthlyVos);junMonthlyVo。setCumulativeUsJun(entity。getCumulativeUsJun());junMonthlyVo。setCostReductAmountJun(entity。getCostReductAmountJun()null?null:entity。getCostReductAmountJun()。toString());junMonthlyVos。add(junMonthlyVo);improtVo。setJune(junMonthlyVos);julyMonthlyVo。setCumulativeUsJul(entity。getCumulativeUsJul());julyMonthlyVo。setCostReductAmountJul(entity。getCostReductAmountJul()null?null:entity。getCostReductAmountJul()。toString());julyMonthlyVos。add(julyMonthlyVo);improtVo。setJuly(julyMonthlyVos);augMonthlyVo。setCumulativeUsAug(entity。getCumulativeUsAug());augMonthlyVo。setCostReductAmountAug(entity。getCostReductAmountAug()null?null:entity。getCostReductAmountAug()。toString());augMonthlyVos。add(augMonthlyVo);improtVo。setAugust(augMonthlyVos);sepMonthlyVo。setCumulativeUsSep(entity。getCumulativeUsSep());sepMonthlyVo。setCostReductAmountSep(entity。getCostReductAmountSep()null?null:entity。getCostReductAmountSep()。toString());sepMonthlyVos。add(sepMonthlyVo);improtVo。setSeptember(sepMonthlyVos);improtVo。getSeptember();octMonthlyVo。setCumulativeUsOct(entity。getCumulativeUsOct());octMonthlyVo。setCostReductAmountOct(entity。getCostReductAmountOct()null?null:entity。getCostReductAmountOct()。toString());octMonthlyVos。add(octMonthlyVo);improtVo。setOctober(octMonthlyVos);novMonthlyVo。setCumulativeUsNov(entity。getCumulativeUsNov());novMonthlyVo。setCostReductAmountNov(entity。getCostReductAmountNov()null?null:entity。getCostReductAmountNov()。toString());novMonthlyVos。add(novMonthlyVo);improtVo。setNovember(novMonthlyVos);decMonthlyVo。setCumulativeUsDec(entity。getCumulativeUsDec());decMonthlyVo。setCostReductAmountDec(entity。getCostReductAmountDec()null?null:entity。getCostReductAmountDec()。toString());decMonthlyVos。add(decMonthlyVo);improtVo。setDecember(decMonthlyVos);ImprotVoList。add(improtVo);}returnImprotVoList;}returnnull;}
第一次写多表头导出,特意记录一下。