From 8eb964c30402ae90b982c562e326673d54eff7b8 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Thu, 5 Sep 2019 20:18:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D03=E7=89=88=20=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E6=97=A0=E6=B3=95=E6=8C=87=E5=AE=9Asheet=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20#533?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/analysis/v03/XlsSaxAnalyser.java | 12 +++++++++++ .../v03/handlers/BofRecordHandler.java | 14 ++++++------- .../metadata/holder/ReadWorkbookHolder.java | 15 +++++++++++++ .../test/temp/simple/RepeatTest.java | 21 ++++++++++++++++++- update.md | 1 + 5 files changed, 54 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java index a5072a1d..ddabd8cc 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java +++ b/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) { diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/BofRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/BofRecordHandler.java index da5967d4..324d3e3e 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/BofRecordHandler.java +++ b/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 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(); diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java index 9b77c8c2..815ef06e 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java +++ b/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(); + 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; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatTest.java b/src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatTest.java index 28af426e..517220e5 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatTest.java +++ b/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(); + } } diff --git a/update.md b/update.md index 0a4387d4..88df3f9c 100644 --- a/update.md +++ b/update.md @@ -1,6 +1,7 @@ # 2.0.0-beta4 * 修改在传入List>判断行数错误 [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)