EasyExcel动态导出多级表头
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c41b31d6923420e03fbdc9aacd5ab834.png)
工具类
/**
* 特殊表头导出方法
* @param response
* @param fileName
* @param sheetName
* @param list
* @param pojoClass
* @throws IOException
*/
public static void exportExcelSpecial(HttpServletResponse response, String fileName, String sheetName, List<?> list,
Class<?> pojoClass,String haredName) throws IOException {
if(StringUtils.isBlank(fileName)){
//当前日期
fileName = DateUtils.format(new Date());
}
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("UTF-8");
fileName = URLEncoder.encode(fileName, "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), pojoClass)
.head(gemComplexHead(haredName))
.sheet(sheetName)
.doWrite(list);
}
/**
* 特殊Excel导出,先sourceList转换成List<targetClass>,再导出
* @param response
* @param fileName
* @param sheetName
* @param sourceList
* @param targetClass
* @throws Exception
*/
public static void exportExcelToTargetSpecial(HttpServletResponse response, String fileName, String sheetName, List<?> sourceList,
Class<?> targetClass,String haredName) throws Exception {
List targetList = new ArrayList<>(sourceList.size());
for(Object source : sourceList){
Object target = targetClass.newInstance();
BeanUtils.copyProperties(source, target);
targetList.add(target);
}
exportExcelSpecial(response, fileName, sheetName, targetList, targetClass,haredName);
}
表头
/**
* 特殊表头 参数可根据实际情况修改
* @return 表头集合
*/
private static List<List<String>> gemComplexHead(String haredName){
/*
* 主要的集合 装表头数据
*/
List<List<String>> list = new ArrayList<>();
/*
* 第一列字段arr1 对应字符集合 head0 第二列字段arr2 对应字符集合 head1 第三列字段arr3 对应字符集合 head2
* 第四列字段arr4 对应字符集合 head3 第五列字段arr5 对应字符集合 head4 第六列字段arr6 对应字符集合 head5
* 第七列字段arr7 对应字符集合 head6 第八列字段arr8 对应字符集合 head7 第九列字段arr9 对应字符集合 head8
*/
String[] arr1={"生产排程表","制造单简称","","项目/工序"};
String[] arr2={"生产排程表",haredName,"设计出图","完工日期"};
String[] arr3={"生产排程表","下单日期","外协加工","工期"};
String[] arr4={"生产排程表","","外协加工","送货日期"};
String[] arr5={"生产排程表","交货日期","备料","备料日期"};
String[] arr6={"生产排程表","","备料","备料返修"};
String[] arr7={"生产排程表","","生产装配/电柜等委外","工期"};
String[] arr8={"生产排程表","计划员","生产装配/电柜等委外","开工日期"};
String[] arr9={"生产排程表","","生产装配/电柜等委外","完工日期"};
//第一列
List<String> head0 = new ArrayList<>();
Collections.addAll(head0, arr1);
//第二列
List<String> head1 = new ArrayList<>();
Collections.addAll(head1, arr2);
//第三列
List<String> head2 = new ArrayList<>();
Collections.addAll(head2, arr3);
//第四列
List<String> head3 = new ArrayList<>();
Collections.addAll(head3, arr4);
//第五列
List<String> head4 = new ArrayList<>();
Collections.addAll(head4, arr5);
//第六列
List<String> head5 = new ArrayList<>();
Collections.addAll(head5, arr6);
//第七列
List<String> head6 = new ArrayList<>();
Collections.addAll(head6, arr7);
//第八列
List<String> head7 = new ArrayList<>();
Collections.addAll(head7, arr8);
//第九列
List<String> head8 = new ArrayList<>();
Collections.addAll(head8, arr9);
list.add(head0);
list.add(head1);
list.add(head2);
list.add(head3);
list.add(head4);
list.add(head5);
list.add(head6);
list.add(head7);
list.add(head8);
return list;
}
调用
@GetMapping("export")
@ApiOperation("导出")
@LogOperation("导出")
public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
long equipId = Long.parseLong(params.get("equipId").toString());
String userName =(String) params.get("userName");
List<ProdScheduleDTO> list = scheduleService.getProdSche(equipId);
Map<String,Object> map =new HashMap<>();
map.put("planUser",userName);
map.put("mfName",list.get(0).getMfName());
map.put("orderDate","");
map.put("deliveryDate","");
ExcelUtils.exportExcelToTargetSpecial(response, null, "生产排程表", list, ProdScheduleExcel.class,list.get(0).getMfName());
}