From e0d718b0ab9b789720c026e339832dedf28d1e1e Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Fri, 30 Aug 2019 12:21:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=A9=BA=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E8=AF=BB=E5=8F=96=E5=8F=AF=E8=83=BD=E4=B8=BA=E7=A9=BA?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../v07/SharedStringsTableHandler.java | 14 ++++-- .../easyexcel/test/temp/Lock2Test.java | 19 +++++-- .../alibaba/easyexcel/test/temp/LockData.java | 22 ++++++++ .../easyexcel/test/temp/LockDataListener.java | 50 +++++++++++++++++++ 4 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 src/test/java/com/alibaba/easyexcel/test/temp/LockData.java create mode 100644 src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java diff --git a/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java index 9d228d2..b4d9177 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java @@ -30,15 +30,23 @@ public class SharedStringsTableHandler extends DefaultHandler { @Override public void startElement(String uri, String localName, String name, Attributes attributes) { - if (SI_TAG.equals(name)) { - currentData = ""; + if (T_TAG.equals(name)) { + currentElementData = null; + } else if (SI_TAG.equals(name)) { + currentData = null; } } @Override public void endElement(String uri, String localName, String name) { if (T_TAG.equals(name)) { - currentData += currentElementData; + if (currentData == null) { + currentData = currentElementData; + } else { + if (currentElementData != null) { + currentData += currentElementData; + } + } } else if (SI_TAG.equals(name)) { readCache.put(currentData); } 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 dd89105..cba9bae 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java @@ -9,7 +9,12 @@ import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.alibaba.easyexcel.test.demo.read.DemoData; +import com.alibaba.easyexcel.test.demo.read.DemoDataListener; +import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.ExcelReader; +import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.fastjson.JSON; @@ -24,16 +29,21 @@ public class Lock2Test { @Test public void test() throws Exception { - File file = new File("D:\\test\\珠海2.xlsx"); + File file = new File("D:\\test\\test001.xlsx"); List list = EasyExcel.read(file).sheet().headRowNumber(0).doReadSync(); LOGGER.info("数据:{}", list.size()); for (Object data : list) { LOGGER.info("返回数据:{}", JSON.toJSONString(data)); } - LOGGER.info("文件状态:{}", file.exists()); - file.delete(); - Thread.sleep(500 * 1000); + } + + @Test + public void simpleRead() { + // 写法1: + String fileName = "D:\\test\\珠海 (1).xlsx"; + // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭 + EasyExcel.read(fileName, LockData.class, new LockDataListener()).sheet().doRead(); } @Test @@ -50,5 +60,4 @@ public class Lock2Test { Thread.sleep(500 * 1000); } - } diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/LockData.java b/src/test/java/com/alibaba/easyexcel/test/temp/LockData.java new file mode 100644 index 0000000..2e1ed7b --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/temp/LockData.java @@ -0,0 +1,22 @@ +package com.alibaba.easyexcel.test.temp; + +import lombok.Data; + +/** + * 基础数据类.这里的排序和excel里面的排序一致 + * + * @author Jiaju Zhuang + **/ +@Data +public class LockData { + private String string0; + private String string1; + private String string2; + private String string3; + private String string4; + private String string5; + private String string6; + private String string7; + private String string8; + +} diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java b/src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java new file mode 100644 index 0000000..bda277c --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java @@ -0,0 +1,50 @@ +package com.alibaba.easyexcel.test.temp; + +import java.util.ArrayList; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.alibaba.easyexcel.test.demo.read.DemoDataListener; +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.event.AnalysisEventListener; +import com.alibaba.fastjson.JSON; + +/** + * 模板的读取类 + * + * @author Jiaju Zhuang + */ +public class LockDataListener extends AnalysisEventListener { + private static final Logger LOGGER = LoggerFactory.getLogger(DemoDataListener.class); + /** + * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 + */ + private static final int BATCH_COUNT = 5; + List list = new ArrayList(); + + @Override + public void invoke(LockData data, AnalysisContext context) { + LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); + list.add(data); + if (list.size() >= BATCH_COUNT) { + saveData(); + list.clear(); + } + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + saveData(); + LOGGER.info("所有数据解析完成!"); + } + + /** + * 加上存储数据库 + */ + private void saveData() { + LOGGER.info("{}条数据,开始存储数据库!", list.size()); + LOGGER.info("存储数据库成功!"); + } +}