Browse Source

Merge pull request #2907 from ls9527/master_分页读取监听器支持自定义分页数

分页读取监听器支持自定义分页条数
pull/2965/head
Jiaju Zhuang 2 years ago committed by GitHub
parent
commit
cbb35d1493
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      easyexcel-core/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java
  2. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java

9
easyexcel-core/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java

@ -27,14 +27,21 @@ public class PageReadListener<T> implements ReadListener<T> {
*/
private final Consumer<List<T>> consumer;
private final int pageSize;
public PageReadListener(Consumer<List<T>> consumer) {
this(consumer, BATCH_COUNT);
}
public PageReadListener(Consumer<List<T>> consumer,int pageSize) {
this.consumer = consumer;
this.pageSize = pageSize;
}
@Override
public void invoke(T data, AnalysisContext context) {
cachedDataList.add(data);
if (cachedDataList.size() >= BATCH_COUNT) {
if (cachedDataList.size() >= pageSize) {
consumer.accept(cachedDataList);
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
}

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java

@ -52,7 +52,7 @@ public class ReadTest {
for (DemoData demoData : dataList) {
log.info("读取到一条数据{}", JSON.toJSONString(demoData));
}
})).sheet().doRead();
},3)).sheet().doRead();
// 写法2:
// 匿名内部类 不用额外写一个DemoDataListener

Loading…
Cancel
Save