From 831275d8a552772befc8812f0b823130e2015f9a Mon Sep 17 00:00:00 2001 From: lbw Date: Tue, 20 Sep 2022 15:44:41 +0800 Subject: [PATCH] =?UTF-8?q?:arrow=5Fup:=20Upgrading=20dependencies.=20easy?= =?UTF-8?q?excel=201.2.6=20fix=20=E5=A4=9Asheet=20=E7=A9=BA=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=95=B0=E7=BB=84=E8=B6=8A=E7=95=8C=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- pom.xml | 2 +- .../handler/AbstractSheetWriteHandler.java | 10 ++++----- .../excel/handler/ManySheetWriteHandler.java | 16 +++++++++++--- .../handler/SingleSheetWriteHandler.java | 22 +++++++++++++------ 5 files changed, 35 insertions(+), 17 deletions(-) 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(); }