Skip to content

Commit 771f0a4

Browse files
committed
修复“sax方式读取excel2003版本,会调用两次doAfterAllAnalysed方法”问题
1 parent 0e3cf48 commit 771f0a4

File tree

5 files changed

+46
-113
lines changed

5 files changed

+46
-113
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* 【core 】 修复ClassScanner自定义classload无效问题(issue#I68TV2@Gitee)
2424
* 【core 】 【重要】删除XmlUtil.readObjectFromXml方法,避免漏洞(issue#2857@Github)
2525
* 【core 】 修复Ipv4Util.list()方法的bug(pr#929@Gitee)
26+
* 【poi 】 修复“sax方式读取excel2003版本,会调用两次doAfterAllAnalysed方法”问题。(pr#919@Gitee)
2627

2728
-------------------------------------------------------------------------------------------------------------
2829

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

-29
Original file line numberDiff line numberDiff line change
@@ -1659,35 +1659,6 @@ 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-
16911662
/**
16921663
* 如果给定集合为空,返回默认集合
16931664
*

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

+12-37
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public void filterRemoveTest() {
306306

307307
final List<String> removed = new ArrayList<>();
308308
final ArrayList<String> filtered = CollUtil.filter(list, t -> {
309-
if("a".equals(t)){
309+
if ("a".equals(t)) {
310310
removed.add(t);
311311
return false;
312312
}
@@ -767,7 +767,7 @@ public void addIfAbsentTest() {
767767
}
768768

769769
@Test
770-
public void mapToMapTest(){
770+
public void mapToMapTest() {
771771
final HashMap<String, String> oldMap = new HashMap<>();
772772
oldMap.put("a", "1");
773773
oldMap.put("b", "12");
@@ -778,9 +778,9 @@ public void mapToMapTest(){
778778
Map.Entry::getKey,
779779
entry -> Long.parseLong(entry.getValue()));
780780

781-
Assert.assertEquals(1L, (long)map.get("a"));
782-
Assert.assertEquals(12L, (long)map.get("b"));
783-
Assert.assertEquals(134L, (long)map.get("c"));
781+
Assert.assertEquals(1L, (long) map.get("a"));
782+
Assert.assertEquals(12L, (long) map.get("b"));
783+
Assert.assertEquals(134L, (long) map.get("c"));
784784
}
785785

786786
@Test
@@ -834,7 +834,7 @@ public void subtractToListTest() {
834834

835835
final List<Long> result = CollUtil.subtractToList(list1, list2);
836836
Assert.assertEquals(1, result.size());
837-
Assert.assertEquals(1L, (long)result.get(0));
837+
Assert.assertEquals(1L, (long) result.get(0));
838838
}
839839

840840
@Test
@@ -845,7 +845,7 @@ public void sortComparableTest() {
845845
}
846846

847847
@Test
848-
public void setValueByMapTest(){
848+
public void setValueByMapTest() {
849849
// https://gitee.com/dromara/hutool/pulls/482
850850
final List<Person> people = Arrays.asList(
851851
new Person("aa", 12, "man", 1),
@@ -886,13 +886,13 @@ public void setValueByMapTest(){
886886
}
887887

888888
@Test
889-
public void distinctTest(){
889+
public void distinctTest() {
890890
final ArrayList<Integer> distinct = CollUtil.distinct(ListUtil.of(5, 3, 10, 9, 0, 5, 10, 9));
891891
Assert.assertEquals(ListUtil.of(5, 3, 10, 9, 0), distinct);
892892
}
893893

894894
@Test
895-
public void distinctByFunctionTest(){
895+
public void distinctByFunctionTest() {
896896
final List<Person> people = Arrays.asList(
897897
new Person("aa", 12, "man", 1),
898898
new Person("bb", 13, "woman", 2),
@@ -941,8 +941,7 @@ public void unionAllNullTest() {
941941
final List<String> list = CollUtil.unionAll(list1, list2, list3);
942942
Assert.assertNotNull(list);
943943

944-
@SuppressWarnings("ConfusingArgumentToVarargsMethod")
945-
final List<String> resList2 = CollUtil.unionAll(null, null, null);
944+
@SuppressWarnings("ConfusingArgumentToVarargsMethod") final List<String> resList2 = CollUtil.unionAll(null, null, null);
946945
Assert.assertNotNull(resList2);
947946
}
948947

@@ -973,8 +972,7 @@ public void unionAllTwoOrdinaryTest() {
973972
public void unionAllOtherIsNullTest() {
974973
final List<Integer> list1 = CollectionUtil.newArrayList(1, 2, 2, 3, 3);
975974
final List<Integer> list2 = CollectionUtil.newArrayList(1, 2, 3);
976-
@SuppressWarnings("ConfusingArgumentToVarargsMethod")
977-
final List<Integer> list = CollUtil.unionAll(list1, list2, null);
975+
@SuppressWarnings("ConfusingArgumentToVarargsMethod") final List<Integer> list = CollUtil.unionAll(list1, list2, null);
978976
Assert.assertNotNull(list);
979977
Assert.assertArrayEquals(
980978
CollectionUtil.newArrayList(1, 2, 2, 3, 3, 1, 2, 3).toArray(),
@@ -1042,32 +1040,9 @@ public Dog(String name, Integer age) {
10421040
}
10431041

10441042
@Test
1045-
public void getFirstTest(){
1043+
public void getFirstTest() {
10461044
final List<?> nullList = null;
10471045
final Object first = CollUtil.getFirst(nullList);
10481046
Assert.assertNull(first);
10491047
}
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-
10731048
}

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

+17-18
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void excel07FromStreamTest() {
5151

5252
@Test
5353
public void excel03Test() {
54-
Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
54+
final Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
5555
reader.read("aaa.xls", 1);
5656

5757
// Console.log("Sheet index: [{}], Sheet name: [{}]", reader.getSheetIndex(), reader.getSheetName());
@@ -60,15 +60,15 @@ public void excel03Test() {
6060

6161
@Test
6262
public void excel03ByNameTest() {
63-
Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
63+
final Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
6464
reader.read("aaa.xls", "校园入学");
6565
reader.read("aaa.xls", "sheetName:校园入学");
6666
}
6767

6868
@Test(expected = POIException.class)
6969
public void excel03ByNameErrorTest() {
7070
// sheet名称不存在则报错
71-
Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
71+
final Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
7272
reader.read("aaa.xls", "校园入学1");
7373
}
7474

@@ -120,42 +120,41 @@ public void handle07CellTest() {
120120
ExcelUtil.readBySax("d:/test/test.xlsx", -1, new RowHandler() {
121121

122122
@Override
123-
public void handleCell(int sheetIndex, long rowIndex, int cellIndex, Object value, CellStyle xssfCellStyle) {
123+
public void handleCell(final int sheetIndex, final long rowIndex, final int cellIndex, final Object value, final CellStyle xssfCellStyle) {
124124
Console.log("{} {} {}", rowIndex, cellIndex, value);
125125
}
126126

127127
@Override
128-
public void handle(int sheetIndex, long rowIndex, List<Object> rowCells) {
128+
public void handle(final int sheetIndex, final long rowIndex, final List<Object> rowCells) {
129129

130130
}
131131
}
132132
);
133133
}
134134

135135
@Test
136-
@Ignore
137136
public void handle03CellTest() {
138-
ExcelUtil.readBySax("d:/test/test.xls", -1, new RowHandler() {
137+
ExcelUtil.readBySax("test.xls", -1, new RowHandler() {
139138

140139
@Override
141-
public void handleCell(int sheetIndex, long rowIndex, int cellIndex, Object value, CellStyle xssfCellStyle) {
142-
Console.log("{} {} {}", rowIndex, cellIndex, value);
140+
public void handleCell(final int sheetIndex, final long rowIndex, final int cellIndex, final Object value, final CellStyle xssfCellStyle) {
141+
//Console.log("{} {} {}", rowIndex, cellIndex, value);
143142
}
144143

145144
@Override
146-
public void handle(int sheetIndex, long rowIndex, List<Object> rowCells) {
145+
public void handle(final int sheetIndex, final long rowIndex, final List<Object> rowCells) {
147146
}
148147
}
149148
);
150149
}
151150

152151
@Test
153152
public void formulaRead03Test() {
154-
List<Object> rows = new ArrayList<>();
153+
final List<Object> rows = new ArrayList<>();
155154
ExcelUtil.readBySax("data_for_sax_test.xls", -1, (i, i1, list) -> {
156-
if(list.size() > 1){
155+
if (list.size() > 1) {
157156
rows.add(list.get(1));
158-
} else{
157+
} else {
159158
rows.add("");
160159
}
161160
});
@@ -164,7 +163,7 @@ public void formulaRead03Test() {
164163

165164
@Test
166165
public void formulaRead07Test() {
167-
List<Object> rows = new ArrayList<>();
166+
final List<Object> rows = new ArrayList<>();
168167
ExcelUtil.readBySax("data_for_sax_test.xlsx", 0, (i, i1, list) ->
169168
rows.add(list.get(1)));
170169

@@ -174,7 +173,7 @@ public void formulaRead07Test() {
174173

175174
@Test
176175
public void dateReadXlsTest() {
177-
List<String> rows = new ArrayList<>();
176+
final List<String> rows = new ArrayList<>();
178177
ExcelUtil.readBySax("data_for_sax_test.xls", 0,
179178
(i, i1, list) -> rows.add(StrUtil.toString(list.get(0)))
180179
);
@@ -188,7 +187,7 @@ public void dateReadXlsTest() {
188187

189188
@Test
190189
public void dateReadXlsxTest() {
191-
List<String> rows = new ArrayList<>();
190+
final List<String> rows = new ArrayList<>();
192191
ExcelUtil.readBySax("data_for_sax_test.xlsx", 0,
193192
(i, i1, list) -> rows.add(StrUtil.toString(list.get(0)))
194193
);
@@ -211,7 +210,7 @@ public void dateReadXlsxTest2() {
211210
@Test
212211
@Ignore
213212
public void readBlankTest() {
214-
File file = new File("D:/test/b.xlsx");
213+
final File file = new File("D:/test/b.xlsx");
215214

216215
ExcelUtil.readBySax(file, 0, (sheetIndex, rowIndex, rowList) -> rowList.forEach(Console::log));
217216

@@ -220,7 +219,7 @@ public void readBlankTest() {
220219

221220
@Test
222221
@Ignore
223-
public void readXlsmTest(){
222+
public void readXlsmTest() {
224223
ExcelUtil.readBySax("d:/test/WhiteListTemplate.xlsm", -1,
225224
(sheetIndex, rowIndex, rowlist) -> Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowlist));
226225
}

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

+16-29
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
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;
83
import cn.hutool.poi.excel.cell.CellLocation;
94
import cn.hutool.poi.excel.sax.handler.RowHandler;
10-
import org.apache.poi.ss.usermodel.CellStyle;
115
import org.junit.Assert;
126
import org.junit.Test;
137

14-
import java.io.FileInputStream;
15-
import java.io.InputStream;
16-
import java.util.ArrayList;
178
import java.util.List;
189
import java.util.Map;
1910
import java.util.concurrent.atomic.AtomicInteger;
@@ -59,39 +50,35 @@ public void toLocationTest() {
5950

6051
@Test
6152
public void readAndWriteTest() {
62-
ExcelReader reader = ExcelUtil.getReader("aaa.xlsx");
63-
ExcelWriter writer = reader.getWriter();
53+
final ExcelReader reader = ExcelUtil.getReader("aaa.xlsx");
54+
final ExcelWriter writer = reader.getWriter();
6455
writer.writeCellValue(1, 2, "设置值");
6556
writer.close();
6657
}
6758

6859
@Test
6960
public void getReaderByBookFilePathAndSheetNameTest() {
70-
ExcelReader reader = ExcelUtil.getReader("aaa.xlsx", "12");
71-
List<Map<String, Object>> list = reader.readAll();
61+
final ExcelReader reader = ExcelUtil.getReader("aaa.xlsx", "12");
62+
final List<Map<String, Object>> list = reader.readAll();
7263
reader.close();
7364
Assert.assertEquals(1L, list.get(1).get("鞋码"));
7465
}
7566

7667
@Test
7768
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-
}
69+
final String path = "readBySax.xls";
70+
final AtomicInteger doAfterAllAnalysedTime = new AtomicInteger(0);
71+
ExcelUtil.readBySax(path, -1, new RowHandler() {
72+
@Override
73+
public void handle(final int sheetIndex, final long rowIndex, final List<Object> rowCells) {
74+
//Console.log("sheetIndex={};rowIndex={},rowCells={}",sheetIndex,rowIndex,rowCells);
75+
}
8676

87-
@Override
88-
public void doAfterAllAnalysed() {
89-
doAfterAllAnalysedTime.addAndGet(1);
90-
}
91-
});
92-
}catch (Exception ex){
93-
ex.printStackTrace();
94-
}
77+
@Override
78+
public void doAfterAllAnalysed() {
79+
doAfterAllAnalysedTime.addAndGet(1);
80+
}
81+
});
9582
//总共2个sheet页,读取所有sheet时,一共执行doAfterAllAnalysed2次。
9683
Assert.assertEquals(2, doAfterAllAnalysedTime.intValue());
9784
}

0 commit comments

Comments
 (0)