mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
添加StopReadException,定义sax读取时用户可手动终止(issue#3820@Github)
This commit is contained in:
@@ -163,6 +163,8 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader<Excel03Sax
|
|||||||
factory.processWorkbookEvents(request, fs);
|
factory.processWorkbookEvents(request, fs);
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
throw new POIException(e);
|
throw new POIException(e);
|
||||||
|
} catch (final StopReadException e) {
|
||||||
|
// issue#3820 跳过,用户抛出此异常,表示强制结束读取
|
||||||
} finally {
|
} finally {
|
||||||
IoUtil.closeQuietly(fs);
|
IoUtil.closeQuietly(fs);
|
||||||
}
|
}
|
||||||
|
@@ -205,6 +205,8 @@ public class ExcelSaxUtil {
|
|||||||
throw new IORuntimeException(e);
|
throw new IORuntimeException(e);
|
||||||
} catch (final SAXException e) {
|
} catch (final SAXException e) {
|
||||||
throw new POIException(e);
|
throw new POIException(e);
|
||||||
|
} catch (final StopReadException e){
|
||||||
|
// issue#3820 跳过,用户抛出此异常,表示强制结束读取
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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]);
|
||||||
|
}
|
||||||
|
}
|
@@ -29,7 +29,8 @@ import java.util.List;
|
|||||||
public interface RowHandler {
|
public interface RowHandler {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理一行数据
|
* 处理一行数据<br>
|
||||||
|
* 如果想结束读取,抛出StopReadException即可
|
||||||
*
|
*
|
||||||
* @param sheetIndex 当前Sheet序号
|
* @param sheetIndex 当前Sheet序号
|
||||||
* @param rowIndex 当前行号,从0开始计数
|
* @param rowIndex 当前行号,从0开始计数
|
||||||
@@ -38,7 +39,8 @@ public interface RowHandler {
|
|||||||
void handle(int sheetIndex, long rowIndex, List<Object> rowCells);
|
void handle(int sheetIndex, long rowIndex, List<Object> rowCells);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理一个单元格的数据
|
* 处理一个单元格的数据<br>
|
||||||
|
* 如果想结束读取,抛出StopReadException即可
|
||||||
*
|
*
|
||||||
* @param sheetIndex 当前Sheet序号
|
* @param sheetIndex 当前Sheet序号
|
||||||
* @param rowIndex 当前行号
|
* @param rowIndex 当前行号
|
||||||
|
@@ -26,6 +26,7 @@ import org.dromara.hutool.core.text.StrUtil;
|
|||||||
import org.dromara.hutool.poi.excel.ExcelUtil;
|
import org.dromara.hutool.poi.excel.ExcelUtil;
|
||||||
import org.dromara.hutool.poi.excel.cell.values.FormulaCellValue;
|
import org.dromara.hutool.poi.excel.cell.values.FormulaCellValue;
|
||||||
import org.dromara.hutool.poi.excel.sax.Excel03SaxReader;
|
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.excel.sax.handler.RowHandler;
|
||||||
import org.dromara.hutool.poi.POIException;
|
import org.dromara.hutool.poi.POIException;
|
||||||
import org.apache.poi.ss.usermodel.CellStyle;
|
import org.apache.poi.ss.usermodel.CellStyle;
|
||||||
@@ -51,6 +52,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() {
|
||||||
// 工具化快速读取
|
// 工具化快速读取
|
||||||
|
Reference in New Issue
Block a user