From f74c8299e9da4bd925565b182010232fd494ea62 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Tue, 23 Jun 2020 19:09:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=B7=B3=E7=9D=80=E8=AF=BB?= =?UTF-8?q?=E5=8F=9603=E7=89=88=E6=9C=AC=E7=A9=BA=E6=8C=87=E9=92=88bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../excel/analysis/v03/XlsSaxAnalyser.java | 8 +- .../easyexcel/test/core/skip/SkipData.java | 15 ++++ .../test/core/skip/SkipDataTest.java | 86 +++++++++++++++++++ update.md | 3 + 5 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 src/test/java/com/alibaba/easyexcel/test/core/skip/SkipData.java create mode 100644 src/test/java/com/alibaba/easyexcel/test/core/skip/SkipDataTest.java diff --git a/pom.xml b/pom.xml index a887a7c..5af71e6 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.alibaba easyexcel - 2.2.5 + 2.2.6 jar easyexcel diff --git a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java index e1c19be..83f9864 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java +++ b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java @@ -70,12 +70,12 @@ import com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder; *

* * To turn an excel file into a CSV or similar, then see * the XLS2CSVmra example * *

- * * * @see XLS2CSVmra + * * * @see XLS2CSVmra * * @author jipengfei */ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { + private static final Logger LOGGER = LoggerFactory.getLogger(XlsSaxAnalyser.class); private static final short DUMMY_RECORD_SID = -1; private XlsReadContext xlsReadContext; @@ -146,8 +146,8 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { if (handler == null) { return; } - boolean ignoreRecord = (handler instanceof IgnorableXlsRecordHandler) - && xlsReadContext.xlsReadSheetHolder() != null && xlsReadContext.xlsReadWorkbookHolder().getIgnoreRecord(); + boolean ignoreRecord = + (handler instanceof IgnorableXlsRecordHandler) && xlsReadContext.xlsReadWorkbookHolder().getIgnoreRecord(); if (ignoreRecord) { // No need to read the current sheet return; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipData.java b/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipData.java new file mode 100644 index 0000000..1476448 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipData.java @@ -0,0 +1,15 @@ +package com.alibaba.easyexcel.test.core.skip; + +import com.alibaba.excel.annotation.ExcelProperty; + +import lombok.Data; + +/** + * @author Jiaju Zhuang + */ +@Data +public class SkipData { + + @ExcelProperty("姓名") + private String name; +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipDataTest.java new file mode 100644 index 0000000..3f62994 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipDataTest.java @@ -0,0 +1,86 @@ +package com.alibaba.easyexcel.test.core.skip; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +import com.alibaba.easyexcel.test.core.simple.SimpleData; +import com.alibaba.easyexcel.test.core.simple.SimpleDataListener; +import com.alibaba.easyexcel.test.core.simple.SimpleDataSheetNameListener; +import com.alibaba.easyexcel.test.util.TestFileUtil; +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.ExcelReader; +import com.alibaba.excel.ExcelWriter; +import com.alibaba.excel.event.SyncReadListener; +import com.alibaba.excel.read.metadata.ReadSheet; +import com.alibaba.excel.write.metadata.WriteSheet; + +/** + * @author Jiaju Zhuang + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class SkipDataTest { + + private static File file07; + private static File file03; + + @BeforeClass + public static void init() { + file07 = TestFileUtil.createNewFile("skip.xlsx"); + file03 = TestFileUtil.createNewFile("skip.xls"); + } + + @Test + public void t01ReadAndWrite07() { + readAndWrite(file07); + } + + @Test + public void t02ReadAndWrite03() { + readAndWrite(file03); + } + + private void readAndWrite(File file) { + ExcelWriter excelWriter = EasyExcel.write(file, SimpleData.class).build(); + WriteSheet writeSheet0 = EasyExcel.writerSheet(0, "第一个").build(); + WriteSheet writeSheet1 = EasyExcel.writerSheet(1, "第二个").build(); + WriteSheet writeSheet2 = EasyExcel.writerSheet(2, "第三个").build(); + WriteSheet writeSheet3 = EasyExcel.writerSheet(3, "第四个").build(); + excelWriter.write(data("name1"), writeSheet0); + excelWriter.write(data("name2"), writeSheet1); + excelWriter.write(data("name3"), writeSheet2); + excelWriter.write(data("name4"), writeSheet3); + excelWriter.finish(); + + List list = EasyExcel.read(file, SkipData.class, null).sheet("第二个").doReadSync(); + Assert.assertEquals(1, list.size()); + Assert.assertEquals("name2", list.get(0).getName()); + + SyncReadListener syncReadListener = new SyncReadListener(); + ExcelReader excelReader = EasyExcel.read(file, SkipData.class, null).registerReadListener(syncReadListener) + .build(); + ReadSheet readSheet1 = EasyExcel.readSheet("第二个").build(); + ReadSheet readSheet3 = EasyExcel.readSheet("第四个").build(); + excelReader.read(readSheet1, readSheet3); + List syncList = syncReadListener.getList(); + Assert.assertEquals(2, syncList.size()); + Assert.assertEquals("name2", ((SkipData) syncList.get(0)).getName()); + Assert.assertEquals("name4", ((SkipData) syncList.get(1)).getName()); + excelReader.finish(); + } + + + private List data(String name) { + List list = new ArrayList(); + SkipData data = new SkipData(); + data.setName(name); + list.add(data); + return list; + } +} diff --git a/update.md b/update.md index 32b34e0..0e377b7 100644 --- a/update.md +++ b/update.md @@ -1,3 +1,6 @@ +# 2.2.6 +* 修改跳着读取03版本空指针bug + # 2.2.5 * `ExcelProperty`新增`order` 用于排序 * 修复导出指定`index`会导致空行的bug