diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/sax/Excel03SaxReader.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/sax/Excel03SaxReader.java index d73bff12d..07f8c4b4c 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/sax/Excel03SaxReader.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/sax/Excel03SaxReader.java @@ -163,6 +163,8 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader + * 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]); + } +} diff --git a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/sax/handler/RowHandler.java b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/sax/handler/RowHandler.java index dc220c4b2..e4a34d7f2 100644 --- a/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/sax/handler/RowHandler.java +++ b/hutool-poi/src/main/java/org/dromara/hutool/poi/excel/sax/handler/RowHandler.java @@ -29,7 +29,8 @@ import java.util.List; public interface RowHandler { /** - * 处理一行数据 + * 处理一行数据
+ * 如果想结束读取,抛出StopReadException即可 * * @param sheetIndex 当前Sheet序号 * @param rowIndex 当前行号,从0开始计数 @@ -38,7 +39,8 @@ public interface RowHandler { void handle(int sheetIndex, long rowIndex, List rowCells); /** - * 处理一个单元格的数据 + * 处理一个单元格的数据
+ * 如果想结束读取,抛出StopReadException即可 * * @param sheetIndex 当前Sheet序号 * @param rowIndex 当前行号 diff --git a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/reader/ExcelSaxReadTest.java b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/reader/ExcelSaxReadTest.java index 29263b785..bdc221aa8 100644 --- a/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/reader/ExcelSaxReadTest.java +++ b/hutool-poi/src/test/java/org/dromara/hutool/poi/excel/reader/ExcelSaxReadTest.java @@ -26,6 +26,7 @@ import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.poi.excel.ExcelUtil; import org.dromara.hutool.poi.excel.cell.values.FormulaCellValue; import org.dromara.hutool.poi.excel.sax.Excel03SaxReader; +import org.dromara.hutool.poi.excel.sax.StopReadException; import org.dromara.hutool.poi.excel.sax.handler.RowHandler; import org.dromara.hutool.poi.POIException; import org.apache.poi.ss.usermodel.CellStyle; @@ -51,6 +52,24 @@ public class ExcelSaxReadTest { 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 public void excel07ByNameTest() { // 工具化快速读取