代码如下,模板类字段是动态的,怎么改造代码: setResponseHeader("项目工单统计-工单统计工单位置", response); //表头 List<List<String>> header = new ArrayList<>(); header.add(Lists.newArrayList("位置", "一级位置")); header.add(Lists.newArrayList("位置", "二级位置")); header.add(Lists.newArrayList("位置", "三级位置")); header.add(Lists.newArrayList("工单总数", "工单总数")); header.add(Lists.newArrayList("进行中工单数", "进行中工单数")); header.add(Lists.newArrayList("已挂起工单数", "已挂起工单数")); header.add(Lists.newArrayList("挂单率", "挂单率")); header.add(Lists.newArrayList("已完成工单数", "已完成工单数")); header.add(Lists.newArrayList("完成率", "完成率")); //组装导出dto List<OrderLocationAnalysisDTO> list = this.getOrderLocationAnalysisDTOList(query); List<Integer> workOrderTypeList = new ArrayList<>(); //公区报单 workOrderTypeList.add(11); WriteTable head = EasyExcel.writerTable(0).needHead(Boolean.TRUE).build(); ExcelWriter writer = EasyExcel.write(response.getOutputStream()).build(); WriteTable data = EasyExcel.writerTable(1).needHead(Boolean.TRUE).head(header).build(); WriteSheet sheet = EasyExcel.writerSheet(0).needHead(Boolean.FALSE).sheetName("工单统计工单位置导出").build(); writer.write(concatHead("工单统计工单位置导出",query.getTimeRange().getStartTime(),workOrderTypeList), sheet, head); writer.write(list,sheet,data); writer.finish();
时间: 2024-02-20 11:58:40 浏览: 134
可以将模板类字段作为参数传入方法中,然后根据参数动态生成表头。修改后的代码如下:
```
public void exportOrderLocationAnalysis(HttpServletResponse response, String sheetName, List<String> templateFields, OrderLocationAnalysisQuery query) throws IOException {
//生成表头
List<List<String>> header = new ArrayList<>();
for (String field : templateFields) {
String[] fieldArr = field.split("-");
header.add(Lists.newArrayList(fieldArr[0], fieldArr[1]));
}
//组装导出dto
List<OrderLocationAnalysisDTO> list = this.getOrderLocationAnalysisDTOList(query);
List<Integer> workOrderTypeList = new ArrayList<>();
//公区报单
workOrderTypeList.add(11);
WriteTable head = EasyExcel.writerTable(0).needHead(Boolean.TRUE).build();
ExcelWriter writer = EasyExcel.write(response.getOutputStream()).build();
WriteTable data = EasyExcel.writerTable(1).needHead(Boolean.TRUE).head(header).build();
WriteSheet sheet = EasyExcel.writerSheet(0).needHead(Boolean.FALSE).sheetName(sheetName).build();
writer.write(concatHead(sheetName, query.getTimeRange().getStartTime(), workOrderTypeList), sheet, head);
writer.write(list, sheet, data);
writer.finish();
}
```
调用该方法时,传入模板类字段的列表即可:
```
List<String> templateFields = Arrays.asList("位置-一级位置", "位置-二级位置", "位置-三级位置", "工单总数-工单总数", "进行中工单数-进行中工单数", "已挂起工单数-已挂起工单数", "挂单率-挂单率", "已完成工单数-已完成工单数", "完成率-完成率");
exportOrderLocationAnalysis(response, "工单统计工单位置导出", templateFields, query);
```
阅读全文