This commit is contained in:
Looly 2025-11-26 23:35:28 +08:00
parent d9cbb19460
commit a56d2c03dc

View File

@ -79,7 +79,7 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader<Excel03Sax
/** /**
* 自定义需要处理的sheet编号如果-1表示处理所有sheet * 自定义需要处理的sheet编号如果-1表示处理所有sheet
*/ */
private int rid = -1; private int sheetIndex = -1;
/** /**
* sheet名称主要用于使用sheet名读取的情况 * sheet名称主要用于使用sheet名读取的情况
*/ */
@ -132,7 +132,7 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader<Excel03Sax
* @throws POIException IO异常包装 * @throws POIException IO异常包装
*/ */
public Excel03SaxReader read(POIFSFileSystem fs, String idOrRidOrSheetName) throws POIException { public Excel03SaxReader read(POIFSFileSystem fs, String idOrRidOrSheetName) throws POIException {
this.rid = getSheetIndex(idOrRidOrSheetName); this.sheetIndex = getSheetIndex(idOrRidOrSheetName);
formatListener = new FormatTrackingHSSFListener(new MissingRecordAwareHSSFListener(this)); formatListener = new FormatTrackingHSSFListener(new MissingRecordAwareHSSFListener(this));
final HSSFRequest request = new HSSFRequest(); final HSSFRequest request = new HSSFRequest();
@ -162,7 +162,7 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader<Excel03Sax
* @return sheet序号 * @return sheet序号
*/ */
public int getSheetIndex() { public int getSheetIndex() {
return this.rid; return this.sheetIndex;
} }
/** /**
@ -175,8 +175,8 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader<Excel03Sax
return this.sheetName; return this.sheetName;
} }
if (this.boundSheetRecords.size() > this.rid) { if (this.boundSheetRecords.size() > this.sheetIndex) {
return this.boundSheetRecords.get(this.rid > -1 ? this.rid : this.curRid).getSheetname(); return this.boundSheetRecords.get(this.sheetIndex > -1 ? this.sheetIndex : this.curRid).getSheetname();
} }
return null; return null;
@ -189,7 +189,7 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader<Excel03Sax
*/ */
@Override @Override
public void processRecord(Record record) { public void processRecord(Record record) {
if (this.rid > -1 && this.curRid > this.rid) { if (this.sheetIndex > -1 && this.curRid > this.sheetIndex) {
// 指定Sheet之后的数据不再处理 // 指定Sheet之后的数据不再处理
return; return;
} }
@ -200,7 +200,7 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader<Excel03Sax
boundSheetRecords.add(boundSheetRecord); boundSheetRecords.add(boundSheetRecord);
final String currentSheetName = boundSheetRecord.getSheetname(); final String currentSheetName = boundSheetRecord.getSheetname();
if(null != this.sheetName && StrUtil.equals(this.sheetName, currentSheetName)){ if(null != this.sheetName && StrUtil.equals(this.sheetName, currentSheetName)){
this.rid = this.boundSheetRecords.size() -1; this.sheetIndex = this.boundSheetRecords.size() -1;
} }
} else if (record instanceof SSTRecord) { } else if (record instanceof SSTRecord) {
// 静态字符串表 // 静态字符串表
@ -215,7 +215,7 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader<Excel03Sax
curRid++; curRid++;
} }
} else if (record instanceof EOFRecord){ } else if (record instanceof EOFRecord){
if(this.rid < 0 && null != this.sheetName){ if(this.sheetIndex < 0 && null != this.sheetName){
throw new POIException("Sheet [{}] not exist!", this.sheetName); throw new POIException("Sheet [{}] not exist!", this.sheetName);
} }
if(this.curRid != -1 && isProcessCurrentSheet()) { if(this.curRid != -1 && isProcessCurrentSheet()) {
@ -369,35 +369,35 @@ public class Excel03SaxReader implements HSSFListener, ExcelSaxReader<Excel03Sax
*/ */
private boolean isProcessCurrentSheet() { private boolean isProcessCurrentSheet() {
// rid < 0 sheet名称存在说明没有匹配到sheet名称 // rid < 0 sheet名称存在说明没有匹配到sheet名称
return (this.rid < 0 && null == this.sheetName) || this.rid == this.curRid; return (this.sheetIndex < 0 && null == this.sheetName) || this.sheetIndex == this.curRid;
} }
/** /**
* 获取sheet索引从0开始 * 获取sheet索引从0开始
* <ul> * <ul>
* <li>传入'rId'开头直接去除rId前缀</li> * <li>传入'rId'开头直接去除rId前缀</li>
* <li>传入纯数字表示sheetIndex直接转换为rid</li> * <li>传入纯数字表示sheetIndex直接使用</li>
* </ul> * </ul>
* *
* @param idOrRidOrSheetName Excel中的sheet id或者rid编号或sheet名称从0开始rid必须加rId前缀例如rId0如果为-1处理所有编号的sheet * @param sheetIndexOrSheetName Excel中的sheet 编号或sheet名称从0开始如果为-1处理所有编号的sheet
* @return sheet索引从0开始 * @return sheet索引从0开始
* @since 5.5.5 * @since 5.5.5
*/ */
private int getSheetIndex(String idOrRidOrSheetName) { private int getSheetIndex(String sheetIndexOrSheetName) {
Assert.notBlank(idOrRidOrSheetName, "id or rid or sheetName must be not blank!"); Assert.notBlank(sheetIndexOrSheetName, "id or rid or sheetName must be not blank!");
// rid直接处理 // rid直接处理
if (StrUtil.startWithIgnoreCase(idOrRidOrSheetName, RID_PREFIX)) { if (StrUtil.startWithIgnoreCase(sheetIndexOrSheetName, RID_PREFIX)) {
return Integer.parseInt(StrUtil.removePrefixIgnoreCase(idOrRidOrSheetName, RID_PREFIX)); return Integer.parseInt(StrUtil.removePrefixIgnoreCase(sheetIndexOrSheetName, RID_PREFIX));
} else if(StrUtil.startWithIgnoreCase(idOrRidOrSheetName, SHEET_NAME_PREFIX)){ } else if(StrUtil.startWithIgnoreCase(sheetIndexOrSheetName, SHEET_NAME_PREFIX)){
// since 5.7.10支持任意名称 // since 5.7.10支持任意名称
this.sheetName = StrUtil.removePrefixIgnoreCase(idOrRidOrSheetName, SHEET_NAME_PREFIX); this.sheetName = StrUtil.removePrefixIgnoreCase(sheetIndexOrSheetName, SHEET_NAME_PREFIX);
} else { } else {
try { try {
return Integer.parseInt(idOrRidOrSheetName); return Integer.parseInt(sheetIndexOrSheetName);
} catch (NumberFormatException ignore) { } catch (NumberFormatException ignore) {
// 如果用于传入非数字按照sheet名称对待 // 如果用于传入非数字按照sheet名称对待
this.sheetName = idOrRidOrSheetName; this.sheetName = sheetIndexOrSheetName;
} }
} }