From 7d19499304c37a7e45a73521be500e8c3f1f8d05 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Fri, 10 Feb 2023 18:49:10 +0800 Subject: [PATCH] =?UTF-8?q?*=20=E5=88=86=E9=A1=B5=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E7=9B=91=E5=90=AC=E5=99=A8=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E5=88=86=E9=A1=B5=E6=9D=A1=E6=95=B0=20[Issue=20#2383]?= =?UTF-8?q?(https://github.com/alibaba/easyexcel/issues/2383)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/read/listener/PageReadListener.java | 15 ++++++++----- .../com/alibaba/excel/util/SheetUtils.java | 9 ++++---- .../test/core/simple/SimpleDataTest.java | 22 +++++++++++++++++-- .../easyexcel/test/demo/read/ReadTest.java | 5 +++-- update.md | 1 + 5 files changed, 38 insertions(+), 14 deletions(-) diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java b/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java index 8c3fe782..b97db357 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java @@ -15,7 +15,7 @@ import org.apache.commons.collections4.CollectionUtils; */ public class PageReadListener implements ReadListener { /** - * Single handle the amount of data + * Defuault single handle the amount of data */ public static int BATCH_COUNT = 100; /** @@ -27,23 +27,26 @@ public class PageReadListener implements ReadListener { */ private final Consumer> consumer; - private final int pageSize; + /** + * Single handle the amount of data + */ + private final int batchCount; public PageReadListener(Consumer> consumer) { this(consumer, BATCH_COUNT); } - public PageReadListener(Consumer> consumer,int pageSize) { + public PageReadListener(Consumer> consumer, int batchCount) { this.consumer = consumer; - this.pageSize = pageSize; + this.batchCount = batchCount; } @Override public void invoke(T data, AnalysisContext context) { cachedDataList.add(data); - if (cachedDataList.size() >= pageSize) { + if (cachedDataList.size() >= batchCount) { consumer.accept(cachedDataList); - cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); + cachedDataList = ListUtils.newArrayListWithExpectedSize(batchCount); } } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/util/SheetUtils.java b/easyexcel-core/src/main/java/com/alibaba/excel/util/SheetUtils.java index c2c727c7..12886c85 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/util/SheetUtils.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/util/SheetUtils.java @@ -20,8 +20,7 @@ public class SheetUtils { /** * Match the parameters to the actual sheet * - * @param readSheet - * actual sheet + * @param readSheet actual sheet * @param analysisContext * @return */ @@ -47,11 +46,13 @@ public class SheetUtils { if (!StringUtils.isEmpty(parameterSheetName)) { boolean autoTrim = (parameterReadSheet.getAutoTrim() != null && parameterReadSheet.getAutoTrim()) || (parameterReadSheet.getAutoTrim() == null - && analysisContext.readWorkbookHolder().getGlobalConfiguration().getAutoTrim()); + && analysisContext.readWorkbookHolder().getGlobalConfiguration().getAutoTrim()); + String sheetName = readSheet.getSheetName(); if (autoTrim) { parameterSheetName = parameterSheetName.trim(); + sheetName = sheetName.trim(); } - match = parameterSheetName.equals(readSheet.getSheetName()); + match = parameterSheetName.equals(sheetName); } } if (match) { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java index 80277545..fcc2ba21 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java @@ -5,11 +5,16 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import com.alibaba.easyexcel.test.demo.read.DemoData; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.read.listener.PageReadListener; import com.alibaba.excel.support.ExcelTypeEnum; +import com.alibaba.fastjson.JSON; +import lombok.extern.slf4j.Slf4j; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.FixMethodOrder; @@ -20,6 +25,7 @@ import org.junit.runners.MethodSorters; * @author Jiaju Zhuang */ @FixMethodOrder(MethodSorters.NAME_ASCENDING) +@Slf4j public class SimpleDataTest { private static File file07; @@ -90,8 +96,20 @@ public class SimpleDataTest { @Test public void t21SheetNameRead07() { - EasyExcel.read(TestFileUtil.readFile("simple" + File.separator + "simple07.xlsx"), SimpleData.class, - new SimpleDataSheetNameListener()).sheet("simple").doRead(); + List> list = EasyExcel.read( + TestFileUtil.readFile("simple" + File.separator + "simple07.xlsx")) + .sheet("simple") + .doReadSync(); + Assert.assertEquals(1, list.size()); + } + + @Test + public void t22PageReadListener07() { + EasyExcel.read(file07, SimpleData.class, + new PageReadListener(dataList -> { + Assert.assertEquals(5, dataList.size()); + }, 5)) + .sheet().doRead(); } private void synchronousRead(File file) { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java index 542fe463..ad72da05 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java @@ -47,12 +47,13 @@ public class ReadTest { // since: 3.0.0-beta1 String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭 - // 这里每次会读取100条数据 然后返回过来 直接调用使用数据就行 + // 这里默认每次会读取100条数据 然后返回过来 直接调用使用数据就行 + // 具体需要返回多少行可以在`PageReadListener`的构造函数设置 EasyExcel.read(fileName, DemoData.class, new PageReadListener(dataList -> { for (DemoData demoData : dataList) { log.info("读取到一条数据{}", JSON.toJSONString(demoData)); } - },3)).sheet().doRead(); + })).sheet().doRead(); // 写法2: // 匿名内部类 不用额外写一个DemoDataListener diff --git a/update.md b/update.md index f118eeec..f315c5b3 100644 --- a/update.md +++ b/update.md @@ -3,6 +3,7 @@ * 兼容`LocalDate` [Issue #2908](https://github.com/alibaba/easyexcel/issues/2908) * 优化大文件内存存储,减少内存占用 [Issue #2657](https://github.com/alibaba/easyexcel/issues/2657) * 在临时文件被删除的情况下能正常的读取 [Issue #2693](https://github.com/alibaba/easyexcel/issues/2693) +* 分页读取监听器支持自定义分页条数 [Issue #2383](https://github.com/alibaba/easyexcel/issues/2383) # 3.2.0