mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix readBySax for xls not support sheetName
This commit is contained in:
@@ -80,9 +80,19 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader<Excel03Sax
|
||||
* 自定义需要处理的sheet编号,如果-1表示处理所有sheet
|
||||
*/
|
||||
private int rid = -1;
|
||||
// 当前rid索引
|
||||
/**
|
||||
* sheet名称,主要用于使用sheet名读取的情况
|
||||
*/
|
||||
private String sheetName;
|
||||
|
||||
/**
|
||||
* 当前rid索引
|
||||
*/
|
||||
private int curRid = -1;
|
||||
|
||||
/**
|
||||
* 行处理器
|
||||
*/
|
||||
private final RowHandler rowHandler;
|
||||
|
||||
/**
|
||||
@@ -159,9 +169,14 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader<Excel03Sax
|
||||
* @return Sheet名
|
||||
*/
|
||||
public String getSheetName() {
|
||||
if(null != this.sheetName){
|
||||
return this.sheetName;
|
||||
}
|
||||
|
||||
if (this.boundSheetRecords.size() > this.rid) {
|
||||
return this.boundSheetRecords.get(this.rid > -1 ? this.rid : this.curRid).getSheetname();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -179,7 +194,11 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader<Excel03Sax
|
||||
|
||||
if (record instanceof BoundSheetRecord) {
|
||||
// Sheet边界记录,此Record中可以获得Sheet名
|
||||
boundSheetRecords.add((BoundSheetRecord) record);
|
||||
final BoundSheetRecord boundSheetRecord = (BoundSheetRecord) record;
|
||||
boundSheetRecords.add(boundSheetRecord);
|
||||
if(this.rid < 0 && null != this.sheetName && StrUtil.equals(this.sheetName, boundSheetRecord.getSheetname())){
|
||||
this.rid = this.boundSheetRecords.size() -1;
|
||||
}
|
||||
} else if (record instanceof SSTRecord) {
|
||||
// 静态字符串表
|
||||
sstRecord = (SSTRecord) record;
|
||||
@@ -193,6 +212,9 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader<Excel03Sax
|
||||
curRid++;
|
||||
}
|
||||
} else if (record instanceof EOFRecord){
|
||||
if(this.rid < 0 && null != this.sheetName){
|
||||
throw new POIException("Sheet [{}] not exist!", this.sheetName);
|
||||
}
|
||||
processLastCellSheet();
|
||||
} else if (isProcessCurrentSheet()) {
|
||||
if (record instanceof MissingCellDummyRecord) {
|
||||
@@ -340,7 +362,8 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader<Excel03Sax
|
||||
* @return 是否处理当前sheet
|
||||
*/
|
||||
private boolean isProcessCurrentSheet() {
|
||||
return this.rid < 0 || this.curRid == this.rid;
|
||||
// rid < 0 且 sheet名称存在,说明没有匹配到sheet名称
|
||||
return (this.rid < 0 && null == this.sheetName) || this.rid == this.curRid;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -365,31 +388,11 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader<Excel03Sax
|
||||
final int sheetIndex;
|
||||
try {
|
||||
return Integer.parseInt(idOrRidOrSheetName);
|
||||
} catch (NumberFormatException e) {
|
||||
// 非数字,可能为sheet名称
|
||||
sheetIndex = getSheetIndexByName(idOrRidOrSheetName);
|
||||
if(sheetIndex > 0){
|
||||
return sheetIndex;
|
||||
}
|
||||
} catch (NumberFormatException ignore) {
|
||||
// 如果用于传入非数字,按照sheet名称对待
|
||||
this.sheetName = idOrRidOrSheetName;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Invalid rId or id or sheetName: " + idOrRidOrSheetName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过sheet名称获取其位置(rid)
|
||||
* @param sheetName sheet名称
|
||||
* @return 位置,-1表示未找到
|
||||
* @since 5.6.6
|
||||
*/
|
||||
private int getSheetIndexByName(String sheetName){
|
||||
final List<BoundSheetRecord> boundSheetRecords = this.boundSheetRecords;
|
||||
final int size = boundSheetRecords.size();
|
||||
for(int i = 0; i < size; i++){
|
||||
if(StrUtil.equals(sheetName, boundSheetRecords.get(i).getSheetname())){
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
// ---------------------------------------------------------------------------------------------- Private method end
|
||||
|
@@ -75,24 +75,24 @@ public interface ExcelSaxReader<T> {
|
||||
* 开始读取Excel
|
||||
*
|
||||
* @param path 文件路径
|
||||
* @param rid Excel中的sheet rid编号,如果为-1处理所有编号的sheet
|
||||
* @param idOrRidOrSheetName Excel中的sheet id或者rid编号或sheet名称,rid必须加rId前缀,例如rId1,如果为-1处理所有编号的sheet
|
||||
* @return this
|
||||
* @throws POIException POI异常
|
||||
*/
|
||||
default T read(String path, int rid) throws POIException {
|
||||
return read(FileUtil.file(path), rid);
|
||||
default T read(String path, int idOrRidOrSheetName) throws POIException {
|
||||
return read(FileUtil.file(path), idOrRidOrSheetName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始读取Excel
|
||||
*
|
||||
* @param path 文件路径
|
||||
* @param rid Excel中的sheet rid编号,如果为-1处理所有编号的sheet
|
||||
* @param idOrRidOrSheetName Excel中的sheet id或者rid编号或sheet名称,rid必须加rId前缀,例如rId1,如果为-1处理所有编号的sheet
|
||||
* @return this
|
||||
* @throws POIException POI异常
|
||||
*/
|
||||
default T read(String path, String rid) throws POIException {
|
||||
return read(FileUtil.file(path), rid);
|
||||
default T read(String path, String idOrRidOrSheetName) throws POIException {
|
||||
return read(FileUtil.file(path), idOrRidOrSheetName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -9,6 +9,7 @@ import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.poi.excel.cell.FormulaCellValue;
|
||||
import cn.hutool.poi.excel.sax.Excel03SaxReader;
|
||||
import cn.hutool.poi.excel.sax.handler.RowHandler;
|
||||
import cn.hutool.poi.exceptions.POIException;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
@@ -41,10 +42,24 @@ public class ExcelSaxReadTest {
|
||||
public void excel03Test() {
|
||||
Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
|
||||
reader.read("aaa.xls", 1);
|
||||
|
||||
// Console.log("Sheet index: [{}], Sheet name: [{}]", reader.getSheetIndex(), reader.getSheetName());
|
||||
ExcelUtil.readBySax("aaa.xls", 1, createRowHandler());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void excel03ByNameTest() {
|
||||
Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
|
||||
reader.read("aaa.xls", "校园入学");
|
||||
}
|
||||
|
||||
@Test(expected = POIException.class)
|
||||
public void excel03ByNameErrorTest() {
|
||||
// sheet名称不存在则报错
|
||||
Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
|
||||
reader.read("aaa.xls", "校园入学1");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void readBlankLineTest() {
|
||||
@@ -124,7 +139,6 @@ public class ExcelSaxReadTest {
|
||||
|
||||
@Test
|
||||
public void formulaRead03Test() {
|
||||
Console.log(FileUtil.file("data_for_sax_test.xls"));
|
||||
List<Object> rows = new ArrayList<>();
|
||||
ExcelUtil.readBySax("data_for_sax_test.xls", -1, (i, i1, list) -> {
|
||||
if(list.size() > 1){
|
||||
|
Reference in New Issue
Block a user