diff --git a/README.md b/README.md index 31930b5..855db0b 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ EasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目 com.pig4cloud.excel excel-spring-boot-starter - 1.2.5 + 1.2.6 ``` diff --git a/pom.xml b/pom.xml index d21863c..1b7b1e0 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ com.pig4cloud.excel excel-spring-boot-starter - 1.2.5 + 1.2.6 excel-spring-boot-starter easy and high performance excel https://pig4cloud.com diff --git a/src/main/java/com/pig4cloud/plugin/excel/handler/AbstractSheetWriteHandler.java b/src/main/java/com/pig4cloud/plugin/excel/handler/AbstractSheetWriteHandler.java index fabfbb1..3197e61 100644 --- a/src/main/java/com/pig4cloud/plugin/excel/handler/AbstractSheetWriteHandler.java +++ b/src/main/java/com/pig4cloud/plugin/excel/handler/AbstractSheetWriteHandler.java @@ -115,11 +115,11 @@ public ExcelWriter getExcelWriter(HttpServletResponse response, ResponseExcel re } if (responseExcel.include().length != 0) { - writerBuilder.includeColumnFiledNames(Arrays.asList(responseExcel.include())); + writerBuilder.includeColumnFieldNames(Arrays.asList(responseExcel.include())); } if (responseExcel.exclude().length != 0) { - writerBuilder.excludeColumnFiledNames(Arrays.asList(responseExcel.exclude())); + writerBuilder.excludeColumnFieldNames(Arrays.asList(responseExcel.exclude())); } if (responseExcel.writeHandler().length != 0) { @@ -197,10 +197,10 @@ else if (isNotInterface(bookHeadEnhancerClass)) { else if (dataClass != null) { writerSheetBuilder.head(dataClass); if (sheet.excludes().length > 0) { - writerSheetBuilder.excludeColumnFiledNames(Arrays.asList(sheet.excludes())); + writerSheetBuilder.excludeColumnFieldNames(Arrays.asList(sheet.excludes())); } if (sheet.includes().length > 0) { - writerSheetBuilder.includeColumnFiledNames(Arrays.asList(sheet.includes())); + writerSheetBuilder.includeColumnFieldNames(Arrays.asList(sheet.includes())); } } @@ -217,7 +217,7 @@ private void fillCustomHeadInfo(Class dataClass, Class eleList = (List) objList.get(i); - Class dataClass = eleList.get(0).getClass(); - // 创建sheet - sheet = this.sheet(sheets[i], dataClass, responseExcel.template(), responseExcel.headGenerator()); + + if (CollectionUtils.isEmpty(eleList)) { + sheet = EasyExcel.writerSheet(responseExcel.sheets()[i].sheetName()).build(); + } + else { + // 有模板则不指定sheet名 + Class dataClass = eleList.get(0).getClass(); + sheet = this.sheet(responseExcel.sheets()[i], dataClass, responseExcel.template(), + responseExcel.headGenerator()); + } + // 填充 sheet if (responseExcel.fill()) { excelWriter.fill(eleList, sheet); diff --git a/src/main/java/com/pig4cloud/plugin/excel/handler/SingleSheetWriteHandler.java b/src/main/java/com/pig4cloud/plugin/excel/handler/SingleSheetWriteHandler.java index a6f7080..e1eaa08 100644 --- a/src/main/java/com/pig4cloud/plugin/excel/handler/SingleSheetWriteHandler.java +++ b/src/main/java/com/pig4cloud/plugin/excel/handler/SingleSheetWriteHandler.java @@ -1,5 +1,6 @@ package com.pig4cloud.plugin.excel.handler; +import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.write.metadata.WriteSheet; @@ -8,6 +9,7 @@ import com.pig4cloud.plugin.excel.enhance.WriterBuilderEnhancer; import com.pig4cloud.plugin.excel.kit.ExcelException; import org.springframework.beans.factory.ObjectProvider; +import org.springframework.util.CollectionUtils; import javax.servlet.http.HttpServletResponse; import java.util.List; @@ -44,21 +46,27 @@ public boolean support(Object obj) { @Override public void write(Object obj, HttpServletResponse response, ResponseExcel responseExcel) { - List list = (List) obj; + List eleList = (List) obj; ExcelWriter excelWriter = getExcelWriter(response, responseExcel); - // 有模板则不指定sheet名 - Class dataClass = list.get(0).getClass(); - WriteSheet sheet = this.sheet(responseExcel.sheets()[0], dataClass, responseExcel.template(), - responseExcel.headGenerator()); + WriteSheet sheet; + if (CollectionUtils.isEmpty(eleList)) { + sheet = EasyExcel.writerSheet(responseExcel.sheets()[0].sheetName()).build(); + } + else { + // 有模板则不指定sheet名 + Class dataClass = eleList.get(0).getClass(); + sheet = this.sheet(responseExcel.sheets()[0], dataClass, responseExcel.template(), + responseExcel.headGenerator()); + } // 填充 sheet if (responseExcel.fill()) { - excelWriter.fill(list, sheet); + excelWriter.fill(eleList, sheet); } else { // 写入sheet - excelWriter.write(list, sheet); + excelWriter.write(eleList, sheet); } excelWriter.finish(); }