Browse Source

修复03版 读取无法指定sheet的问题 #533

bugfix
Jiaju Zhuang 5 years ago
parent
commit
8eb964c304
  1. 12
      src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
  2. 14
      src/main/java/com/alibaba/excel/analysis/v03/handlers/BofRecordHandler.java
  3. 15
      src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java
  4. 21
      src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatTest.java
  5. 1
      update.md

12
src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java

@ -14,6 +14,8 @@ import org.apache.poi.hssf.eventusermodel.HSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFRequest;
import org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener;
import org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord;
import org.apache.poi.hssf.record.BOFRecord;
import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
@ -83,6 +85,7 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelExecutor {
@Override
public void execute() {
analysisContext.readSheetHolder().getSheetNo();
MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);
formatListener = new FormatTrackingHSSFListener(listener);
workbookBuildingListener = new EventWorkbookBuilder.SheetRecordCollectingListener(formatListener);
@ -121,6 +124,10 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelExecutor {
@Override
public void processRecord(Record record) {
// Not data from the current sheet
if (ignoreRecord(record)) {
return;
}
int thisRow = -1;
int thisColumn = -1;
CellData cellData = null;
@ -164,6 +171,11 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelExecutor {
processLastCellOfRow(record);
}
private boolean ignoreRecord(Record record) {
return analysisContext.readWorkbookHolder().getIgnoreRecord03() && record.getSid() != BoundSheetRecord.sid
&& record.getSid() != BOFRecord.sid;
}
private void processLastCellOfRow(Record record) {
// Handle end of row
if (record instanceof LastCellOfRowDummyRecord) {

14
src/main/java/com/alibaba/excel/analysis/v03/handlers/BofRecordHandler.java

@ -23,7 +23,6 @@ public class BofRecordHandler extends AbstractXlsRecordHandler {
private int sheetIndex;
private List<ReadSheet> sheets;
private AnalysisContext context;
private boolean analyAllSheet;
private EventWorkbookBuilder.SheetRecordCollectingListener workbookBuildingListener;
public BofRecordHandler(EventWorkbookBuilder.SheetRecordCollectingListener workbookBuildingListener,
@ -48,21 +47,20 @@ public class BofRecordHandler extends AbstractXlsRecordHandler {
if (orderedBsrs == null) {
orderedBsrs = BoundSheetRecord.orderByBofPosition(boundSheetRecords);
}
sheetIndex++;
ReadSheet readSheet = new ReadSheet(sheetIndex, orderedBsrs[sheetIndex - 1].getSheetname());
ReadSheet readSheet = new ReadSheet(sheetIndex, orderedBsrs[sheetIndex].getSheetname());
sheets.add(readSheet);
if (this.analyAllSheet) {
context.currentSheet(null, readSheet);
if (sheetIndex == context.readSheetHolder().getSheetNo()) {
context.readWorkbookHolder().setIgnoreRecord03(Boolean.FALSE);
} else {
context.readWorkbookHolder().setIgnoreRecord03(Boolean.TRUE);
}
sheetIndex++;
}
}
}
@Override
public void init() {
if (context.readSheetHolder() == null) {
this.analyAllSheet = true;
}
sheetIndex = 0;
orderedBsrs = null;
boundSheetRecords.clear();

15
src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java

@ -101,6 +101,12 @@ public class ReadWorkbookHolder extends AbstractReadHolder {
*/
private POIFSFileSystem poifsFileSystem;
/**
* Excel 2003 cannot read specific sheet. It can only read sheet by sheet.So when you specify one sheet, you ignore
* the others.
*/
private Boolean ignoreRecord03;
public ReadWorkbookHolder(ReadWorkbook readWorkbook) {
super(readWorkbook, null, readWorkbook.getConvertAllFiled());
this.readWorkbook = readWorkbook;
@ -144,6 +150,7 @@ public class ReadWorkbookHolder extends AbstractReadHolder {
this.defaultReturnMap = readWorkbook.getDefaultReturnMap();
}
this.hasReadSheet = new HashSet<Integer>();
this.ignoreRecord03 = Boolean.FALSE;
}
public ReadWorkbook getReadWorkbook() {
@ -258,6 +265,14 @@ public class ReadWorkbookHolder extends AbstractReadHolder {
this.poifsFileSystem = poifsFileSystem;
}
public Boolean getIgnoreRecord03() {
return ignoreRecord03;
}
public void setIgnoreRecord03(Boolean ignoreRecord03) {
this.ignoreRecord03 = ignoreRecord03;
}
@Override
public HolderEnum holderType() {
return HolderEnum.WORKBOOK;

21
src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatTest.java

@ -30,10 +30,29 @@ public class RepeatTest {
EasyExcel.read(new FileInputStream("D:\\test\\hg2.xls"), LockData.class, new RepeatListener())
.headRowNumber(0).build();
ReadSheet r1 = EasyExcel.readSheet(0).build();
ReadSheet r2 = EasyExcel.readSheet(1).build();
ReadSheet r2 = EasyExcel.readSheet(2).build();
reader.read(r1);
reader.read(r2);
reader.finish();
}
@Test
public void hh2() throws IOException {
ExcelReader reader =
EasyExcel.read(new FileInputStream("D:\\test\\sheet.xls"), LockData.class, new RepeatListener())
.headRowNumber(0).build();
ReadSheet r2 = EasyExcel.readSheet(1).build();
reader.read(r2);
reader.finish();
}
@Test
public void hh1() throws IOException {
ExcelReader reader =
EasyExcel.read(new FileInputStream("D:\\test\\hg2.xls"), LockData.class, new RepeatListener())
.headRowNumber(0).build();
ReadSheet r2 = EasyExcel.readSheet(0).build();
reader.read(r2);
reader.finish();
}
}

1
update.md

@ -1,6 +1,7 @@
# 2.0.0-beta4
* 修改在传入List<List<Object>>判断行数错误 [Issue #526](https://github.com/alibaba/easyexcel/issues/526)
* 修复在mac 2016 2017导出的excel 可能存在多余字段的问题
* 修复03版 读取无法指定sheet的问题 [Issue #533](https://github.com/alibaba/easyexcel/issues/533)
# 2.0.0-beta3
* 导出完成移除临时目录 [Issue #386](https://github.com/alibaba/easyexcel/issues/386)

Loading…
Cancel
Save