1.添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.0-beta3</version>
</dependency>
2.
@PostMapping("/bigexport")
public void bigExport(String filter,Integer state,String startTime,String endTime,Long[]ids, HttpServletResponse response) throws UnsupportedEncodingException {
policyInterestedService.bigExport(filter,state,startTime,endTime,ids,response);
}
3.需要导出字段添加注解
@Override
public void bigExport(String filter, Integer state, String startTime, String endTime, Long[] ids, HttpServletResponse response) throws UnsupportedEncodingException {
if(StringUtils.isNotBlank(filter)){
filter = StringUtils.join("%",filter,"%");
}
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("ORD" + DateTime.now().toString("yyyyMMddhhmmss"), "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
//模板类
List<ApplicationTemplateVo> applicationTemplateVos = applicationTemplateMapper.getList(filter,state,startTime,endTime,ids);
log.info(JSON.toJSONString(applicationTemplateVos));
//高企申报类
List<HighEnterpriseExportVo> highEnterpriseExportVos = highEnterpriseMapper.export(filter,state,startTime,endTime,ids);
highEnterpriseExportVos.stream().map(highEnterpriseExportVo -> {
// 发明专利
List<Invention> inventions = highEnterpriseExportVo.getInventions();
List<String> inventionPatentList = inventions.stream().map(invention -> invention.getInventionPatent()).collect(Collectors.toList());
log.info(JSON.toJSONString(inventionPatentList));
String inventionPatents = StringUtils.join(inventionPatentList, ",");
highEnterpriseExportVo.setInventionPatentStrings(inventionPatents);
//著作
List<Work> works = highEnterpriseExportVo.getWorks();
if(!CollectionUtils.isEmpty(works)){
List<String> workList = works.stream().map(work -> work.getWorkPatent()).collect(Collectors.toList());
String workPatentS = StringUtils.join(workList, ",");
highEnterpriseExportVo.setWorkPatentStrings(workPatentS);
}
return highEnterpriseExportVo;
}).collect(Collectors.toList());
//工商注册类
List<IndustrialCommercialRegisterVo> industrialCommercialRegisterVos = industrialCommercialRegisterMapper.export(filter,state,startTime,endTime,ids);
//法律服务类
List<LegalServiceVo> legalServiceVos = legalServiceMapper.export(filter, state, startTime, endTime, ids);
legalServiceVos.stream().map(legalServiceVo -> {
// 1:聘用通知,2:法律咨询
codeToMessageUtil.setServiceTypeString(legalServiceVo);
// 1:企业纠纷,2:股东纠纷,3:劳务纠纷,4:侵权,5:其他
codeToMessageUtil.setSericeItemString(legalServiceVo);
return legalServiceVo;
}).collect(Collectors.toList());
//科技项目类
List<ScienceProjectsVo> scienceProjectsVos = scienceProjectsMapper.export(filter,state,startTime,endTime,ids);
//贷款贴息类
List<SubsidizedInterestVo> subsidizedInterestVos = subsidizedInterestMapper.export(filter,state,startTime,endTime,ids);
//人才服务类
List<TalentIntroductionVo> talentIntroductionVos = talentIntroductionMapper.export(filter,state,startTime,endTime,ids);
try {
OutputStream os = response.getOutputStream();
ExcelWriter excelWriter = EasyExcel.write(os).build();
WriteSheet test1 = EasyExcel.writerSheet(0, "test1").head(ApplicationTemplateVo.class).build();
WriteSheet test2 = EasyExcel.writerSheet(1, "test2").head(HighEnterpriseExportVo.class).build();
WriteSheet test3 = EasyExcel.writerSheet(2, "test3").head(IndustrialCommercialRegisterVo.class).build();
WriteSheet test4 = EasyExcel.writerSheet(3, "test4").head(LegalServiceVo.class).build();
WriteSheet test5 = EasyExcel.writerSheet(4, "test5").head(ScienceProjectsVo.class).build();
WriteSheet test6 = EasyExcel.writerSheet(5, "test6").head(SubsidizedInterestVo.class).build();
WriteSheet test7 = EasyExcel.writerSheet(6, "test7").head(TalentIntroductionVo.class).build();
excelWriter.write(applicationTemplateVos,test1)
.write(highEnterpriseExportVos,test2)
.write(industrialCommercialRegisterVos,test3)
.write(legalServiceVos,test4)
.write(scienceProjectsVos,test5)
.write(subsidizedInterestVos,test6)
.write(talentIntroductionVos,test7);
excelWriter.finish();
} catch (IOException e) {
e.printStackTrace();
}
}