From 852f0ecacb7552430d40fc0b1c457e561cde4e4d Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Fri, 22 May 2020 17:23:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=9A=E4=B8=AA`sheet`?= =?UTF-8?q?=E4=B8=8D=E6=8C=89=E7=85=A7=E9=A1=BA=E5=BA=8F=E5=86=99=E5=85=A5?= =?UTF-8?q?=20=E4=BC=9A=E9=94=99=E8=AF=AF=E7=9A=84bug=20#1332?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/context/WriteContextImpl.java | 19 ++++-- .../metadata/holder/WriteWorkbookHolder.java | 1 + .../easyexcel/test/temp/Lock2Test.java | 2 +- .../easyexcel/test/temp/WriteV33Test.java | 64 +++++++++++++++++++ update.md | 3 + 5 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 src/test/java/com/alibaba/easyexcel/test/temp/WriteV33Test.java diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java index 0af631c..cec99f5 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java @@ -112,7 +112,6 @@ public class WriteContextImpl implements WriteContext { return; } - initCurrentSheetHolder(writeSheet); // Workbook handler need to supplementary execution @@ -125,7 +124,11 @@ public class WriteContextImpl implements WriteContext { private boolean selectSheetFromCache(WriteSheet writeSheet) { writeSheetHolder = null; - if (writeSheet.getSheetNo() != null) { + Integer sheetNo = null; + if (writeSheet.getSheetNo() == null && StringUtils.isEmpty(writeSheet.getSheetName())) { + sheetNo = 0; + } + if (sheetNo != null) { writeSheetHolder = writeWorkbookHolder.getHasBeenInitializedSheetIndexMap().get(writeSheet.getSheetNo()); } if (writeSheetHolder == null && !StringUtils.isEmpty(writeSheet.getSheetName())) { @@ -160,9 +163,15 @@ public class WriteContextImpl implements WriteContext { Sheet currentSheet; try { if (writeSheetHolder.getSheetNo() != null) { - currentSheet = writeWorkbookHolder.getWorkbook().getSheetAt(writeSheetHolder.getSheetNo()); - writeSheetHolder - .setCachedSheet(writeWorkbookHolder.getCachedWorkbook().getSheetAt(writeSheetHolder.getSheetNo())); + int realSheetNo = writeSheetHolder.getSheetNo(); + // When the add default sort order of appearance + if (WriteTypeEnum.ADD.equals(writeType)) { + currentSheet = createSheet(); + } else { + currentSheet = writeWorkbookHolder.getWorkbook().getSheetAt(realSheetNo); + writeSheetHolder + .setCachedSheet(writeWorkbookHolder.getCachedWorkbook().getSheetAt(realSheetNo)); + } } else { // sheet name must not null currentSheet = writeWorkbookHolder.getWorkbook().getSheet(writeSheetHolder.getSheetName()); diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java index be557eb..8c84f1d 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java @@ -112,6 +112,7 @@ public class WriteWorkbookHolder extends AbstractWriteHolder { */ private Boolean writeExcelOnException; + public WriteWorkbookHolder(WriteWorkbook writeWorkbook) { super(writeWorkbook, null, writeWorkbook.getConvertAllFiled()); this.writeWorkbook = writeWorkbook; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java b/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java index 2771e58..88d665a 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java @@ -32,7 +32,7 @@ public class Lock2Test { @Test public void test() throws Exception { - File file = TestFileUtil.readUserHomeFile("test/test2style.xls"); + File file = TestFileUtil.readUserHomeFile("test/test.xlsx"); List list = EasyExcel.read(file).ignoreEmptyRow(true).sheet().headRowNumber(0).doReadSync(); LOGGER.info("数据:{}", list.size()); diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/WriteV33Test.java b/src/test/java/com/alibaba/easyexcel/test/temp/WriteV33Test.java new file mode 100644 index 0000000..ca4f3ba --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/temp/WriteV33Test.java @@ -0,0 +1,64 @@ +package com.alibaba.easyexcel.test.temp; + +import java.io.File; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.apache.poi.hssf.util.CellReference; +import org.apache.poi.ss.usermodel.FillPatternType; +import org.apache.poi.ss.usermodel.IndexedColors; +import org.junit.Ignore; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.alibaba.easyexcel.test.demo.write.DemoData; +import com.alibaba.easyexcel.test.util.TestFileUtil; +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.ExcelWriter; +import com.alibaba.excel.write.metadata.WriteSheet; +import com.alibaba.excel.write.metadata.style.WriteCellStyle; +import com.alibaba.excel.write.metadata.style.WriteFont; +import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; +import com.alibaba.fastjson.JSON; + +/** + * 临时测试 + * + * @author Jiaju Zhuang + **/ +@Ignore +public class WriteV33Test { + + private static final Logger LOGGER = LoggerFactory.getLogger(WriteV33Test.class); + + @Test + public void test() throws Exception { + // 方法2 如果写到不同的sheet 同一个对象 + String fileName = TestFileUtil.getPath() + "repeatedWrite" + System.currentTimeMillis() + ".xlsx"; + // 这里 指定文件 + ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build(); + WriteSheet writeSheet1 = EasyExcel.writerSheet(1, "模板1").build(); + WriteSheet writeSheet2 = EasyExcel.writerSheet(2, "模板2").build(); + WriteSheet writeSheet3 = EasyExcel.writerSheet(3, "模板3").build(); + excelWriter.write(data(2), writeSheet2); + excelWriter.write(data(3), writeSheet3); + excelWriter.write(data(1), writeSheet1); + excelWriter.write(data(3), writeSheet3); + excelWriter.write(data(1), writeSheet1); + /// 千万别忘记finish 会帮忙关闭流 + excelWriter.finish(); + } + + private List data(int no) { + List list = new ArrayList(); + for (int i = 0; i < 10; i++) { + DemoData data = new DemoData(); + data.setString("字符串" + no + "---" + i); + list.add(data); + } + return list; + } + +} diff --git a/update.md b/update.md index 5113607..c27358e 100644 --- a/update.md +++ b/update.md @@ -2,6 +2,9 @@ * 撤销删除`AbstractMergeStrategy` * 修改默认用String读取数字不使用科学计数法 通过`useScientificFormat`修改 * 修复07版仅有样式的空行 默认不忽略的bug +* 写入`sheet`不设置`index`和`name`默认不为0的问题 +* 修复多个`sheet`不按照顺序写入 会错误的bug [Issue #1332](https://github.com/alibaba/easyexcel/issues/1332) + # 2.2.3 * 修改填充数据空数据的bug [Issue #1274](https://github.com/alibaba/easyexcel/issues/1274)