导出2

@PostMapping("/reportPolicy/export")
@ApiOperation(value = "政策库excel导出", notes = "政策库excel导出", httpMethod = "POST")
public void reportPolicy(Page<RepoPolicy> page, @RequestBody RepoPolicy repoPolicy, HttpServletRequest request, HttpServletResponse response) {
repoPolicyService.exportExcel(page,repoPolicy,request,response);
}

@Override
public void exportExcel(Page<RepoPolicy> page, RepoPolicy repoPolicy, HttpServletRequest request, HttpServletResponse response) {
//前端传RepoPolicy类中类型为string数组的fields字段
String[] rowNamesEn = repoPolicy.getFields();
if (ObjectUtil.isNull(rowNamesEn)){
rowNamesEn = new String[1];
}
String[] rowNames = new String[rowNamesEn.length];
List<String> ens = Arrays.asList(ROW_NAMES_EN);
for (int i = 0; i < rowNamesEn.length; i++) {
if (ens.indexOf(rowNamesEn[i]) == -1) {
break;
}
rowNames[i] = ROW_NAMES[ens.indexOf(rowNamesEn[i])];
}
// 自定义标题别名,换成链表表头顺序会固定
Map<String, String> headerAlias = ExcelUtil.getFirstRow(rowNames, rowNamesEn, ExcelUtil.OUT);
// 导出不允许超过1000条
LambdaQueryWrapper<RepoPolicy> wrapper = new LambdaQueryWrapper<>();
if (StrUtil.isNotBlank(repoPolicy.getFileName())) {
wrapper.like(RepoPolicy::getFileName, repoPolicy.getFileName());
}
page.setSize(1000L);
IPage<RepoPolicy> page1 = this.getBaseMapper().selectPage(page, wrapper);
List<RepoPolicy> repoPolicyList = page1.getRecords();

Map<String, Map<String, String>> dictItemLabels = sysLabelService.getLabelItemLabels(DICT_TYPE);
for (RepoPolicy rp : repoPolicyList) {
rp.setId(rp.getId());
rp.setDispatchDept(rp.getDispatchDept());
rp.setSymbol(rp.getSymbol());
rp.setPublishTime(rp.getPublishTime());
rp.setStartTime(rp.getStartTime());
rp.setEndTime(rp.getEndTime());
rp.setFileName(rp.getFileName());
rp.setIntroduction(rp.getIntroduction());
rp.setPolicySort(rp.getPolicySort());
rp.setRegionalLevel(dictItemLabels.getOrDefault("REGIONAL_LEVEL",new HashMap<>(1)).getOrDefault(rp.getRegionalLevel(),""));

String[] industryTypeStrings = rp.getIndustryType().split(",");
List<String> its = Arrays.asList(industryTypeStrings);
List<String> it = new ArrayList<>();
its.forEach(x -> {
String objectQuality = dictItemLabels.getOrDefault("PROFESSION_TYPE",new HashMap<>(1)).getOrDefault(x,"");
it.add(objectQuality);
});
rp.setIndustryType(StringUtils.strip(it.toString(),"[]"));


String[] objectScaleStrings = rp.getObjectScale().split(",");
List<String> oss = Arrays.asList(objectScaleStrings);
List<String> os = new ArrayList<>();
oss.forEach(x -> {
String objectQuality = dictItemLabels.getOrDefault("OBJECT_SCALE",new HashMap<>(1)).getOrDefault(x,"");
os.add(objectQuality);
});
rp.setObjectScale(StringUtils.strip(os.toString(),"[]"));

String[] supportTypeStrings = rp.getSupportType().split(",");
List<String> sts = Arrays.asList(supportTypeStrings);
List<String> st = new ArrayList<>();
sts.forEach(x -> {
String objectQuality = dictItemLabels.getOrDefault("SUPPORT_TYPE",new HashMap<>(1)).getOrDefault(x,"");
st.add(objectQuality);
});
rp.setSupportType(StringUtils.strip(st.toString(),"[]"));
}

// 多sheet的导出,关闭流变成了专门一个方法,后续考虑整合到common
ExcelWriter writer = null;
OutputStream out = null;
try {
writer = cn.hutool.poi.excel.ExcelUtil.getWriter(true);
List<ExcelStyle> excelStyleList = new ArrayList<>(2);
excelStyleList.add(new ExcelStyle(writer, headerAlias, repoPolicyList, "政策库", 1));
ImportTemplateUtils.setStyle(excelStyleList);
// 设置返回头
ImportTemplateUtils.setResponse(response, ImportTemplateUtils.getNameWithRequest(request, "policy.xls"));
out = response.getOutputStream();
writer.flush(out, true);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭writer,释放内存
IoUtil.close(writer);
// 此处记得关闭输出Servlet流
IoUtil.close(out);
}


}
posted @ 2022-05-30 15:26  全琪俊  阅读(38)  评论(0编辑  收藏  举报