Skip to content

Commit fe74f26

Browse files
committed
添加StopReadException,定义sax读取时用户可手动终止(issue#3820@Github)
1 parent c45232e commit fe74f26

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* 【core 】 DateUtil.parseUTC方法标记废弃,改名为parseISO8601(issue#IBB6I5@Gitee)
1313
* 【core 】 添加EnumUtil#getBy(Class, Func1, Object)方法(pr#1283@Gitee)
1414
* 【db 】 添加Entity.addCondition方法(issue#IBCDL2@Gitee)
15+
* 【poi 】 添加StopReadException,定义sax读取时用户可手动终止(issue#3820@Github)
1516

1617
### 🐞Bug修复
1718
* 【crypto 】 修复JWTSignerUtil.createSigner中algorithmId未转换问题(issue#3806@Github)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ public Excel03SaxReader read(POIFSFileSystem fs, String idOrRidOrSheetName) thro
147147
factory.processWorkbookEvents(request, fs);
148148
} catch (IOException e) {
149149
throw new POIException(e);
150+
} catch (final StopReadException e) {
151+
// issue#3820 跳过,用户抛出此异常,表示强制结束读取
150152
} finally {
151153
IoUtil.close(fs);
152154
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public class ExcelSaxUtil {
4747
*/
4848
public static ExcelSaxReader<?> createSaxReader(boolean isXlsx, RowHandler rowHandler) {
4949
return isXlsx
50-
? new Excel07SaxReader(rowHandler)
51-
: new Excel03SaxReader(rowHandler);
50+
? new Excel07SaxReader(rowHandler)
51+
: new Excel03SaxReader(rowHandler);
5252
}
5353

5454
/**
@@ -184,6 +184,8 @@ public static void readFrom(InputStream xmlDocStream, ContentHandler handler) th
184184
throw new IORuntimeException(e);
185185
} catch (SAXException e) {
186186
throw new POIException(e);
187+
} catch (final StopReadException e) {
188+
// issue#3820 跳过,用户抛出此异常,表示强制结束读取
187189
}
188190
}
189191

@@ -268,7 +270,7 @@ private static Number getNumberValue(String value, String numFmtString) {
268270

269271
// issue#IB0EJ9 可能精度丢失,对含有小数的value判断并转为BigDecimal
270272
final double number = Double.parseDouble(value);
271-
if(StrUtil.contains(value, CharUtil.DOT) && !value.equals(Double.toString(number))){
273+
if (StrUtil.contains(value, CharUtil.DOT) && !value.equals(Double.toString(number))) {
272274
// 精度丢失
273275
return NumberUtil.toBigDecimal(value);
274276
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cn.hutool.poi.excel.sax;
2+
3+
import cn.hutool.poi.exceptions.POIException;
4+
5+
/**
6+
* 读取结束异常,用于标记读取结束<br>
7+
* Sax方式读取时,如果用户在RowHandler中抛出此异常,表示读取结束,此时不再读取其他数据
8+
*
9+
* @author Looly
10+
* @since 5.8.35
11+
*/
12+
public class StopReadException extends POIException {
13+
private static final long serialVersionUID = 1L;
14+
15+
/**
16+
* 构造
17+
*
18+
*/
19+
public StopReadException() {
20+
this("Stop read by user.");
21+
}
22+
23+
/**
24+
* 构造
25+
*
26+
* @param message 消息
27+
*/
28+
public StopReadException(final String message) {
29+
super(message);
30+
// 去除堆栈
31+
setStackTrace(new StackTraceElement[0]);
32+
}
33+
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import cn.hutool.core.util.StrUtil;
99
import cn.hutool.poi.excel.cell.FormulaCellValue;
1010
import cn.hutool.poi.excel.sax.Excel03SaxReader;
11+
import cn.hutool.poi.excel.sax.StopReadException;
1112
import cn.hutool.poi.excel.sax.handler.RowHandler;
1213
import cn.hutool.poi.exceptions.POIException;
1314
import org.apache.poi.ss.usermodel.CellStyle;
@@ -33,6 +34,24 @@ public void excel07Test() {
3334
ExcelUtil.readBySax("aaa.xlsx", 0, createRowHandler());
3435
}
3536

37+
@Test
38+
void readEndByExceptionTest(){
39+
ExcelUtil.readBySax("aaa.xlsx", 0, (sheetIndex, rowIndex, rowList) -> {
40+
if (rowIndex == 1) {
41+
throw new StopReadException();
42+
}
43+
});
44+
}
45+
46+
@Test
47+
void readEndByException03Test(){
48+
ExcelUtil.readBySax("aaa.xls", 0, (sheetIndex, rowIndex, rowList) -> {
49+
if (rowIndex == 1) {
50+
throw new StopReadException();
51+
}
52+
});
53+
}
54+
3655
@Test
3756
public void excel07ByNameTest() {
3857
// 工具化快速读取

0 commit comments

Comments
 (0)