动态表头(动态合并表头 & 横向无限扩展列)
//基于Student对象的配置完成表头合并(静态) public void witeExcel(String mainTitle, Class <?> excelBeanClass) { Field[] fields = excelBeanClass.getDeclaredFields(); for(Field field: fields) { ExcelProperty excelProperty = field.getDeclaredAnnotation(ExcelProperty.class); if(excelProperty != null) {//打了ExcelProperty注解的字段处理 try { InvocationHandler excelH = Proxy.getInvocationHandler(excelProperty); Field excelF = excelH.getClass().getDeclaredField("memberValues"); excelF.setAccessible(true); Map < String, Object > excelPropertyValues = (Map<String, Object>) excelF.get(excelH); excelPropertyValues.put("value", new String[] {mainTitle, excelProperty.value()[0]}); } catch(Exception e) { //TODO:异常处理 } } } } //基于Student对象对表头进行动态合并(动态) public void dynamicHeaderByExcelProperty(String mainTitle, String secondTitle, Class <? > excelBeanClass) { Field[] fields = excelBeanClass.getDeclaredFields(); for(Field field: fields) { ExcelProperty excelProperty = field.getDeclaredAnnotation(ExcelProperty.class); if(excelProperty != null) { try { InvocationHandler excelH = Proxy.getInvocationHandler(excelProperty); Field excelF = excelH.getClass().getDeclaredField("memberValues"); excelF.setAccessible(true); Map <String, Object> excelPropertyValues = (Map <String, Object> ) excelF.get(excelH); excelPropertyValues.put("value", new String[] { mainTitle, secondTitle, excelProperty.value()[0]}); } catch(Exception e) { //TODO:异常处理 } } } } //自定义表头的单级动态合并及横向无限扩展列 public List<List<String>> dynamicHeaderByCustom(String mainTitle, Class <?> excelBeanClass) { List<List<String>> headList = new ArrayList<>(); Field[] fields = excelBeanClass.getDeclaredFields(); for(Field field: fields) { ExcelProperty excelProperty = field.getDeclaredAnnotation(ExcelProperty.class); if(excelProperty != null) { List <String> fieldHeadList = new ArrayList<> (); // TODO:待优化扩展,指定不同列的合并 if(StringUtils.isNotBlank(mainTitle)) { fieldHeadList.add(mainTitle); } fieldHeadList.addAll(Arrays.asList(excelProperty.value())); headList.add(fieldHeadList); } } return headList; } //自定义表头的多级动态合并及横向无限扩展列 public List<List<String>> dynamicHeaderByCustom(String mainTitle, List <Class<?>> excelBeanClassList) { List<List<String>> headList = new ArrayList<>(); System.out.println("excelBeanClassList.size()=" + excelBeanClassList.size()); excelBeanClassList.forEach(v - > { headList.addAll(this.dynamicHeaderByCustom(mainTitle, v)); }); return headList; } public static void main(String[] args) { List < List <? >> data = new ArrayList(); ...... System.out.println("download url is :" + exportExcel("成绩", "科目", data, "Excel名称")); } 姓名 成绩 A卷(动态) B卷(动态)学科 分数 填空题 判断题 问答题 附加题 填空题 判断题 问答题 附加题
AAA 数学 110 20分 15分 45分 10分 20分 20分 50分 10分
多sheet页
public void witeExcel(Class<?> excelBeanClass, String title, List<ReportSheetInfo> sheets) { ExcelWriterBuilder write; if(excelBeanClass != null) { write = EasyExcel.write(filePath, targetClass); } else { write = EasyExcel.write(filePath); } ExcelWriter excelWriter = write.build(); sheets.forEach(v - > { ExcelWriterSheetBuilder writeSheet = EasyExcel.writerSheet(sheets.indexOf(v), v.getSheetTitle()); writeSheet.head(v.getHead()); if(CollectionUtils.isNotEmpty(v.getWriteHandlerList())) { v.getWriteHandlerList().forEach(writeSheet::registerWriteHandler); } excelWriter.write(v.getDataList(), writeSheet.build()); }); excelWriter.finish(); write.autoCloseStream(true); } // ReportSheetInfo类中定义writeHandlerList、dataList、head集合 public static void main(String[] args) { List<ReportSheetInfo> sheets = new ArrayList(); ...... System.out.println("download url is :" + exportExcel(Student.class, "Excel名称", sheets)); } 以上是关于EasyExcel的导出Excel封装,部分源码已贴出,需要完整源码的可以在下方评论留言,今天分享就到这里。。。。。。