Browse Source

* 分页读取监听器支持自定义分页条数 [Issue #2383](https://github.com/alibaba/easyexcel/issues/2383)

pull/2965/head
Jiaju Zhuang 2 years ago
parent
commit
7d19499304
  1. 15
      easyexcel-core/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java
  2. 7
      easyexcel-core/src/main/java/com/alibaba/excel/util/SheetUtils.java
  3. 22
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java
  4. 5
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java
  5. 1
      update.md

15
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<T> implements ReadListener<T> { public class PageReadListener<T> implements ReadListener<T> {
/** /**
* Single handle the amount of data * Defuault single handle the amount of data
*/ */
public static int BATCH_COUNT = 100; public static int BATCH_COUNT = 100;
/** /**
@ -27,23 +27,26 @@ public class PageReadListener<T> implements ReadListener<T> {
*/ */
private final Consumer<List<T>> consumer; private final Consumer<List<T>> consumer;
private final int pageSize; /**
* Single handle the amount of data
*/
private final int batchCount;
public PageReadListener(Consumer<List<T>> consumer) { public PageReadListener(Consumer<List<T>> consumer) {
this(consumer, BATCH_COUNT); this(consumer, BATCH_COUNT);
} }
public PageReadListener(Consumer<List<T>> consumer,int pageSize) { public PageReadListener(Consumer<List<T>> consumer, int batchCount) {
this.consumer = consumer; this.consumer = consumer;
this.pageSize = pageSize; this.batchCount = batchCount;
} }
@Override @Override
public void invoke(T data, AnalysisContext context) { public void invoke(T data, AnalysisContext context) {
cachedDataList.add(data); cachedDataList.add(data);
if (cachedDataList.size() >= pageSize) { if (cachedDataList.size() >= batchCount) {
consumer.accept(cachedDataList); consumer.accept(cachedDataList);
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); cachedDataList = ListUtils.newArrayListWithExpectedSize(batchCount);
} }
} }

7
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 * Match the parameters to the actual sheet
* *
* @param readSheet * @param readSheet actual sheet
* actual sheet
* @param analysisContext * @param analysisContext
* @return * @return
*/ */
@ -48,10 +47,12 @@ public class SheetUtils {
boolean autoTrim = (parameterReadSheet.getAutoTrim() != null && parameterReadSheet.getAutoTrim()) boolean autoTrim = (parameterReadSheet.getAutoTrim() != null && parameterReadSheet.getAutoTrim())
|| (parameterReadSheet.getAutoTrim() == null || (parameterReadSheet.getAutoTrim() == null
&& analysisContext.readWorkbookHolder().getGlobalConfiguration().getAutoTrim()); && analysisContext.readWorkbookHolder().getGlobalConfiguration().getAutoTrim());
String sheetName = readSheet.getSheetName();
if (autoTrim) { if (autoTrim) {
parameterSheetName = parameterSheetName.trim(); parameterSheetName = parameterSheetName.trim();
sheetName = sheetName.trim();
} }
match = parameterSheetName.equals(readSheet.getSheetName()); match = parameterSheetName.equals(sheetName);
} }
} }
if (match) { if (match) {

22
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.io.FileOutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert; import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
@ -20,6 +25,7 @@ import org.junit.runners.MethodSorters;
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING)
@Slf4j
public class SimpleDataTest { public class SimpleDataTest {
private static File file07; private static File file07;
@ -90,8 +96,20 @@ public class SimpleDataTest {
@Test @Test
public void t21SheetNameRead07() { public void t21SheetNameRead07() {
EasyExcel.read(TestFileUtil.readFile("simple" + File.separator + "simple07.xlsx"), SimpleData.class, List<Map<Integer, Object>> list = EasyExcel.read(
new SimpleDataSheetNameListener()).sheet("simple").doRead(); 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<SimpleData>(dataList -> {
Assert.assertEquals(5, dataList.size());
}, 5))
.sheet().doRead();
} }
private void synchronousRead(File file) { private void synchronousRead(File file) {

5
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 // since: 3.0.0-beta1
String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭 // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
// 这里每次会读取100条数据 然后返回过来 直接调用使用数据就行 // 这里默认每次会读取100条数据 然后返回过来 直接调用使用数据就行
// 具体需要返回多少行可以在`PageReadListener`的构造函数设置
EasyExcel.read(fileName, DemoData.class, new PageReadListener<DemoData>(dataList -> { EasyExcel.read(fileName, DemoData.class, new PageReadListener<DemoData>(dataList -> {
for (DemoData demoData : dataList) { for (DemoData demoData : dataList) {
log.info("读取到一条数据{}", JSON.toJSONString(demoData)); log.info("读取到一条数据{}", JSON.toJSONString(demoData));
} }
},3)).sheet().doRead(); })).sheet().doRead();
// 写法2: // 写法2:
// 匿名内部类 不用额外写一个DemoDataListener // 匿名内部类 不用额外写一个DemoDataListener

1
update.md

@ -3,6 +3,7 @@
* 兼容`LocalDate` [Issue #2908](https://github.com/alibaba/easyexcel/issues/2908) * 兼容`LocalDate` [Issue #2908](https://github.com/alibaba/easyexcel/issues/2908)
* 优化大文件内存存储,减少内存占用 [Issue #2657](https://github.com/alibaba/easyexcel/issues/2657) * 优化大文件内存存储,减少内存占用 [Issue #2657](https://github.com/alibaba/easyexcel/issues/2657)
* 在临时文件被删除的情况下能正常的读取 [Issue #2693](https://github.com/alibaba/easyexcel/issues/2693) * 在临时文件被删除的情况下能正常的读取 [Issue #2693](https://github.com/alibaba/easyexcel/issues/2693)
* 分页读取监听器支持自定义分页条数 [Issue #2383](https://github.com/alibaba/easyexcel/issues/2383)
# 3.2.0 # 3.2.0

Loading…
Cancel
Save