Skip to content

Commit

Permalink
修复03版 读取无法指定sheet的问题 #533
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuangjiaju committed Sep 5, 2019
1 parent cd2e0fe commit 8eb964c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
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;
Expand Down Expand Up @@ -83,6 +85,7 @@ public List<ReadSheet> sheetList() {

@Override
public void execute() {
analysisContext.readSheetHolder().getSheetNo();
MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);
formatListener = new FormatTrackingHSSFListener(listener);
workbookBuildingListener = new EventWorkbookBuilder.SheetRecordCollectingListener(formatListener);
Expand Down Expand Up @@ -121,6 +124,10 @@ private void init() {

@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;
Expand Down Expand Up @@ -164,6 +171,11 @@ public void processRecord(Record record) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -48,21 +47,20 @@ public void processRecord(Record record) {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -144,6 +150,7 @@ public ReadWorkbookHolder(ReadWorkbook readWorkbook) {
this.defaultReturnMap = readWorkbook.getDefaultReturnMap();
}
this.hasReadSheet = new HashSet<Integer>();
this.ignoreRecord03 = Boolean.FALSE;
}

public ReadWorkbook getReadWorkbook() {
Expand Down Expand Up @@ -258,6 +265,14 @@ public void setPoifsFileSystem(POIFSFileSystem poifsFileSystem) {
this.poifsFileSystem = poifsFileSystem;
}

public Boolean getIgnoreRecord03() {
return ignoreRecord03;
}

public void setIgnoreRecord03(Boolean ignoreRecord03) {
this.ignoreRecord03 = ignoreRecord03;
}

@Override
public HolderEnum holderType() {
return HolderEnum.WORKBOOK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,29 @@ public void hh() throws IOException {
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 change: 1 addition & 0 deletions update.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit 8eb964c

Please sign in to comment.