mirror of
https://gitee.com/dromara/hutool.git
synced 2025-06-28 13:34:09 +08:00
添加StopReadException,定义sax读取时用户可手动终止(issue#3820@Github)
This commit is contained in:
parent
c45232e235
commit
fe74f26f6f
@ -12,6 +12,7 @@
|
|||||||
* 【core 】 DateUtil.parseUTC方法标记废弃,改名为parseISO8601(issue#IBB6I5@Gitee)
|
* 【core 】 DateUtil.parseUTC方法标记废弃,改名为parseISO8601(issue#IBB6I5@Gitee)
|
||||||
* 【core 】 添加EnumUtil#getBy(Class, Func1, Object)方法(pr#1283@Gitee)
|
* 【core 】 添加EnumUtil#getBy(Class, Func1, Object)方法(pr#1283@Gitee)
|
||||||
* 【db 】 添加Entity.addCondition方法(issue#IBCDL2@Gitee)
|
* 【db 】 添加Entity.addCondition方法(issue#IBCDL2@Gitee)
|
||||||
|
* 【poi 】 添加StopReadException,定义sax读取时用户可手动终止(issue#3820@Github)
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【crypto 】 修复JWTSignerUtil.createSigner中algorithmId未转换问题(issue#3806@Github)
|
* 【crypto 】 修复JWTSignerUtil.createSigner中algorithmId未转换问题(issue#3806@Github)
|
||||||
|
@ -147,6 +147,8 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader<Excel03Sax
|
|||||||
factory.processWorkbookEvents(request, fs);
|
factory.processWorkbookEvents(request, fs);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new POIException(e);
|
throw new POIException(e);
|
||||||
|
} catch (final StopReadException e) {
|
||||||
|
// issue#3820 跳过,用户抛出此异常,表示强制结束读取
|
||||||
} finally {
|
} finally {
|
||||||
IoUtil.close(fs);
|
IoUtil.close(fs);
|
||||||
}
|
}
|
||||||
|
@ -47,8 +47,8 @@ public class ExcelSaxUtil {
|
|||||||
*/
|
*/
|
||||||
public static ExcelSaxReader<?> createSaxReader(boolean isXlsx, RowHandler rowHandler) {
|
public static ExcelSaxReader<?> createSaxReader(boolean isXlsx, RowHandler rowHandler) {
|
||||||
return isXlsx
|
return isXlsx
|
||||||
? new Excel07SaxReader(rowHandler)
|
? new Excel07SaxReader(rowHandler)
|
||||||
: new Excel03SaxReader(rowHandler);
|
: new Excel03SaxReader(rowHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -184,6 +184,8 @@ public class ExcelSaxUtil {
|
|||||||
throw new IORuntimeException(e);
|
throw new IORuntimeException(e);
|
||||||
} catch (SAXException e) {
|
} catch (SAXException e) {
|
||||||
throw new POIException(e);
|
throw new POIException(e);
|
||||||
|
} catch (final StopReadException e) {
|
||||||
|
// issue#3820 跳过,用户抛出此异常,表示强制结束读取
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -268,7 +270,7 @@ public class ExcelSaxUtil {
|
|||||||
|
|
||||||
// issue#IB0EJ9 可能精度丢失,对含有小数的value判断并转为BigDecimal
|
// issue#IB0EJ9 可能精度丢失,对含有小数的value判断并转为BigDecimal
|
||||||
final double number = Double.parseDouble(value);
|
final double number = Double.parseDouble(value);
|
||||||
if(StrUtil.contains(value, CharUtil.DOT) && !value.equals(Double.toString(number))){
|
if (StrUtil.contains(value, CharUtil.DOT) && !value.equals(Double.toString(number))) {
|
||||||
// 精度丢失
|
// 精度丢失
|
||||||
return NumberUtil.toBigDecimal(value);
|
return NumberUtil.toBigDecimal(value);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,33 @@
|
|||||||
|
package cn.hutool.poi.excel.sax;
|
||||||
|
|
||||||
|
import cn.hutool.poi.exceptions.POIException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取结束异常,用于标记读取结束<br>
|
||||||
|
* Sax方式读取时,如果用户在RowHandler中抛出此异常,表示读取结束,此时不再读取其他数据
|
||||||
|
*
|
||||||
|
* @author Looly
|
||||||
|
* @since 5.8.35
|
||||||
|
*/
|
||||||
|
public class StopReadException extends POIException {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public StopReadException() {
|
||||||
|
this("Stop read by user.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造
|
||||||
|
*
|
||||||
|
* @param message 消息
|
||||||
|
*/
|
||||||
|
public StopReadException(final String message) {
|
||||||
|
super(message);
|
||||||
|
// 去除堆栈
|
||||||
|
setStackTrace(new StackTraceElement[0]);
|
||||||
|
}
|
||||||
|
}
|
@ -8,6 +8,7 @@ import cn.hutool.core.lang.Console;
|
|||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.poi.excel.cell.FormulaCellValue;
|
import cn.hutool.poi.excel.cell.FormulaCellValue;
|
||||||
import cn.hutool.poi.excel.sax.Excel03SaxReader;
|
import cn.hutool.poi.excel.sax.Excel03SaxReader;
|
||||||
|
import cn.hutool.poi.excel.sax.StopReadException;
|
||||||
import cn.hutool.poi.excel.sax.handler.RowHandler;
|
import cn.hutool.poi.excel.sax.handler.RowHandler;
|
||||||
import cn.hutool.poi.exceptions.POIException;
|
import cn.hutool.poi.exceptions.POIException;
|
||||||
import org.apache.poi.ss.usermodel.CellStyle;
|
import org.apache.poi.ss.usermodel.CellStyle;
|
||||||
@ -33,6 +34,24 @@ public class ExcelSaxReadTest {
|
|||||||
ExcelUtil.readBySax("aaa.xlsx", 0, createRowHandler());
|
ExcelUtil.readBySax("aaa.xlsx", 0, createRowHandler());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void readEndByExceptionTest(){
|
||||||
|
ExcelUtil.readBySax("aaa.xlsx", 0, (sheetIndex, rowIndex, rowList) -> {
|
||||||
|
if (rowIndex == 1) {
|
||||||
|
throw new StopReadException();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void readEndByException03Test(){
|
||||||
|
ExcelUtil.readBySax("aaa.xls", 0, (sheetIndex, rowIndex, rowList) -> {
|
||||||
|
if (rowIndex == 1) {
|
||||||
|
throw new StopReadException();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void excel07ByNameTest() {
|
public void excel07ByNameTest() {
|
||||||
// 工具化快速读取
|
// 工具化快速读取
|
||||||
|
Loading…
Reference in New Issue
Block a user