添加StopReadException,定义sax读取时用户可手动终止(issue#3820@Github)

This commit is contained in:
Looly
2024-12-21 13:09:08 +08:00
parent 35505dfbf8
commit 5dea0ed2b5
5 changed files with 76 additions and 2 deletions

View File

@@ -163,6 +163,8 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader<Excel03Sax
factory.processWorkbookEvents(request, fs);
} catch (final IOException e) {
throw new POIException(e);
} catch (final StopReadException e) {
// issue#3820 跳过,用户抛出此异常,表示强制结束读取
} finally {
IoUtil.closeQuietly(fs);
}

View File

@@ -205,6 +205,8 @@ public class ExcelSaxUtil {
throw new IORuntimeException(e);
} catch (final SAXException e) {
throw new POIException(e);
} catch (final StopReadException e){
// issue#3820 跳过,用户抛出此异常,表示强制结束读取
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright (c) 2013-2024 Hutool Team and hutool.cn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.hutool.poi.excel.sax;
import org.dromara.hutool.poi.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]);
}
}

View File

@@ -29,7 +29,8 @@ import java.util.List;
public interface RowHandler {
/**
* 处理一行数据
* 处理一行数据<br>
* 如果想结束读取抛出StopReadException即可
*
* @param sheetIndex 当前Sheet序号
* @param rowIndex 当前行号从0开始计数
@@ -38,7 +39,8 @@ public interface RowHandler {
void handle(int sheetIndex, long rowIndex, List<Object> rowCells);
/**
* 处理一个单元格的数据
* 处理一个单元格的数据<br>
* 如果想结束读取抛出StopReadException即可
*
* @param sheetIndex 当前Sheet序号
* @param rowIndex 当前行号

View File

@@ -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() {
// 工具化快速读取