From b675ba50c522815b3b0c6241ce07b13ebf1cf83c Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Wed, 16 Oct 2019 19:46:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=B2=A1=E6=9C=89=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E7=9A=84demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/demo/read/NoModleDataListener.java | 50 +++++++++++++++++++ .../easyexcel/test/demo/read/ReadTest.java | 9 ++++ .../easyexcel/test/demo/write/WriteTest.java | 24 ++++++++- 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/alibaba/easyexcel/test/demo/read/NoModleDataListener.java diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/NoModleDataListener.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/NoModleDataListener.java new file mode 100644 index 00000000..2e775387 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/NoModleDataListener.java @@ -0,0 +1,50 @@ +package com.alibaba.easyexcel.test.demo.read; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.event.AnalysisEventListener; +import com.alibaba.fastjson.JSON; + +/** + * 直接用map接收数据 + * + * @author Jiaju Zhuang + */ +public class NoModleDataListener extends AnalysisEventListener> { + private static final Logger LOGGER = LoggerFactory.getLogger(NoModleDataListener.class); + /** + * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 + */ + private static final int BATCH_COUNT = 5; + List> list = new ArrayList>(); + + @Override + public void invoke(Map 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("存储数据库成功!"); + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java index 5665432d..9ebafa4f 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java @@ -217,4 +217,13 @@ public class ReadTest { } } + /** + * 不创建对象的读,不是特别推荐使用,都用String接收对日期的支持不是很好 + */ + @Test + public void noModleRead() { + String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; + // 这里 只要,然后读取第一个sheet 同步读取会自动finish + EasyExcel.read(fileName, new NoModleDataListener()).sheet().doRead(); + } } diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java index 336a1991..2582f105 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java @@ -58,7 +58,6 @@ public class WriteTest { ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build(); WriteSheet writeSheet = EasyExcel.writerSheet("模板").build(); excelWriter.write(data(), writeSheet); - // 进行加密 /// 千万别忘记finish 会帮忙关闭流 excelWriter.finish(); } @@ -379,6 +378,17 @@ public class WriteTest { .registerWriteHandler(new CustomCellWriteHandler()).sheet("模板").doWrite(data()); } + /** + * 不创建对象的写 + */ + @Test + public void noModleWrite() { + // 写法1 + String fileName = TestFileUtil.getPath() + "noModleWrite" + System.currentTimeMillis() + ".xlsx"; + // 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 + EasyExcel.write(fileName).head(head()).sheet("模板").doWrite(dataList()); + } + private List dataLong() { List list = new ArrayList(); for (int i = 0; i < 10; i++) { @@ -405,6 +415,18 @@ public class WriteTest { return list; } + private List> dataList() { + List> list = new ArrayList>(); + for (int i = 0; i < 10; i++) { + List data = new ArrayList(); + data.add("字符串" + i); + data.add(new Date()); + data.add(0.56); + list.add(data); + } + return list; + } + private List data() { List list = new ArrayList(); for (int i = 0; i < 10; i++) {