OrderLocationAnalysisDTO这个是模板类,字段是不固定的
时间: 2024-02-20 20:58:43 浏览: 44
如果OrderLocationAnalysisDTO这个模板类的字段是动态的,可以通过反射来获取字段名,然后动态生成表头。修改后的代码如下:
```
public void exportOrderLocationAnalysis(HttpServletResponse response, String sheetName, Class<?> clazz, OrderLocationAnalysisQuery query) throws IOException {
//生成表头
List<List<String>> header = new ArrayList<>();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
String fieldName = field.getName();
if (!"serialVersionUID".equals(fieldName)) {
header.add(Lists.newArrayList(fieldName, fieldName));
}
}
//组装导出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();
}
```
调用该方法时,传入模板类的Class对象即可:
```
exportOrderLocationAnalysis(response, "工单统计工单位置导出", OrderLocationAnalysisDTO.class, query);
```
阅读全文