Skip to content

Commit 0e3cf48

Browse files
loolygitee-org
authored andcommitted
!919 修复“sax方式读取excel2003版本,会调用两次doAfterAllAnalysed方法”问题。
Merge pull request !919 from hellozrh/v5-dev
2 parents f1164ff + 9ceac4d commit 0e3cf48

File tree

6 files changed

+94
-1
lines changed

6 files changed

+94
-1
lines changed

hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java

+29
Original file line numberDiff line numberDiff line change
@@ -1659,6 +1659,35 @@ public static boolean isEmpty(Collection<?> collection) {
16591659
return collection == null || collection.isEmpty();
16601660
}
16611661

1662+
/**
1663+
* 集合是否为空。
1664+
* 如果集合中所有元素为null或空串,也认为此集合为空。
1665+
* @param collection
1666+
* @return
1667+
*/
1668+
public static boolean isBlank(Collection<?> collection) {
1669+
if(isEmpty(collection)){
1670+
return true;
1671+
}
1672+
1673+
for(Object o: collection){
1674+
if(ObjectUtil.isNotEmpty(o)){
1675+
return false;
1676+
}
1677+
}
1678+
return true;
1679+
}
1680+
1681+
/**
1682+
* 集合是否为非空。
1683+
* 集合长度大于0,且所有元素中至少有一个不为null或空串。
1684+
* @param collection
1685+
* @return
1686+
*/
1687+
public static boolean isNotBlank(Collection<?> collection) {
1688+
return false == isBlank(collection);
1689+
}
1690+
16621691
/**
16631692
* 如果给定集合为空,返回默认集合
16641693
*

hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java

+23
Original file line numberDiff line numberDiff line change
@@ -1047,4 +1047,27 @@ public void getFirstTest(){
10471047
final Object first = CollUtil.getFirst(nullList);
10481048
Assert.assertNull(first);
10491049
}
1050+
1051+
@Test
1052+
public void blankTest() {
1053+
List<String> strs = new ArrayList<>();
1054+
strs.add(null);
1055+
strs.add("");
1056+
strs.add("");
1057+
1058+
boolean c = CollUtil.isBlank(strs);
1059+
Assert.assertEquals(true, c );
1060+
1061+
1062+
List<String> arrs = new ArrayList<>();
1063+
arrs.add(null);
1064+
arrs.add("");
1065+
arrs.add(" ");
1066+
arrs.add("");
1067+
arrs.add(" a ");
1068+
1069+
boolean d = CollUtil.isNotBlank(arrs);
1070+
Assert.assertEquals(true, d );
1071+
}
1072+
10501073
}

hutool-poi/src/main/java/cn/hutool/poi/excel/sax/Excel03SaxReader.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package cn.hutool.poi.excel.sax;
22

3+
import cn.hutool.core.collection.CollUtil;
34
import cn.hutool.core.io.IoUtil;
45
import cn.hutool.core.lang.Assert;
6+
import cn.hutool.core.util.ArrayUtil;
57
import cn.hutool.core.util.ObjectUtil;
68
import cn.hutool.core.util.StrUtil;
9+
import cn.hutool.log.StaticLog;
710
import cn.hutool.poi.excel.sax.handler.RowHandler;
811
import cn.hutool.poi.exceptions.POIException;
912
import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder.SheetRecordCollectingListener;
@@ -216,7 +219,10 @@ public void processRecord(Record record) {
216219
if(this.rid < 0 && null != this.sheetName){
217220
throw new POIException("Sheet [{}] not exist!", this.sheetName);
218221
}
219-
processLastCellSheet();
222+
if(this.curRid != -1 && isProcessCurrentSheet()) {
223+
//只有在当前指定的sheet中,才触发结束事件,且curId=-1时也不处理,避免重复调用
224+
processLastCellSheet();
225+
}
220226
} else if (isProcessCurrentSheet()) {
221227
if (record instanceof MissingCellDummyRecord) {
222228
// 空值的操作

hutool-poi/src/test/java/cn/hutool/poi/excel/ExcelUtilTest.java

+35
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
package cn.hutool.poi.excel;
22

3+
import cn.hutool.core.collection.CollUtil;
4+
import cn.hutool.core.collection.CollectionUtil;
5+
import cn.hutool.core.util.ArrayUtil;
6+
import cn.hutool.core.util.ObjectUtil;
7+
import cn.hutool.core.util.StrUtil;
38
import cn.hutool.poi.excel.cell.CellLocation;
9+
import cn.hutool.poi.excel.sax.handler.RowHandler;
10+
import org.apache.poi.ss.usermodel.CellStyle;
411
import org.junit.Assert;
512
import org.junit.Test;
613

14+
import java.io.FileInputStream;
15+
import java.io.InputStream;
16+
import java.util.ArrayList;
717
import java.util.List;
818
import java.util.Map;
19+
import java.util.concurrent.atomic.AtomicInteger;
920

1021
public class ExcelUtilTest {
1122

@@ -61,4 +72,28 @@ public void getReaderByBookFilePathAndSheetNameTest() {
6172
reader.close();
6273
Assert.assertEquals(1L, list.get(1).get("鞋码"));
6374
}
75+
76+
@Test
77+
public void doAfterAllAnalysedTest() {
78+
String path = "readBySax.xls";
79+
AtomicInteger doAfterAllAnalysedTime = new AtomicInteger(0);
80+
try{
81+
ExcelUtil.readBySax(path, -1, new RowHandler() {
82+
@Override
83+
public void handle(int sheetIndex, long rowIndex, List<Object> rowCells) {
84+
System.out.println(StrUtil.format("sheetIndex={};rowIndex={},rowCells={}",sheetIndex,rowIndex,rowCells));
85+
}
86+
87+
@Override
88+
public void doAfterAllAnalysed() {
89+
doAfterAllAnalysedTime.addAndGet(1);
90+
}
91+
});
92+
}catch (Exception ex){
93+
ex.printStackTrace();
94+
}
95+
//总共2个sheet页,读取所有sheet时,一共执行doAfterAllAnalysed2次。
96+
Assert.assertEquals(2, doAfterAllAnalysedTime.intValue());
97+
}
98+
6499
}
26 KB
Binary file not shown.
23 KB
Binary file not shown.

0 commit comments

Comments
 (0)