Commit 6ed4189bf079b6ab2787c2ca1efbfd030a21c753
1 parent
c14fb4cc
Exists in
master
support hidden sheet
Showing
12 changed files
with
200 additions
and
92 deletions
Show diff stats
src/main/java/com/taover/easyexcel/analysis/v03/XlsSaxAnalyser.java
| 1 | package com.taover.easyexcel.analysis.v03; | 1 | package com.taover.easyexcel.analysis.v03; |
| 2 | 2 | ||
| 3 | import java.io.IOException; | 3 | import java.io.IOException; |
| 4 | -import java.util.ArrayList; | ||
| 5 | import java.util.HashMap; | 4 | import java.util.HashMap; |
| 6 | import java.util.List; | 5 | import java.util.List; |
| 7 | import java.util.Map; | 6 | import java.util.Map; |
| @@ -54,9 +53,11 @@ import com.taover.easyexcel.analysis.v03.handlers.NoteRecordHandler; | @@ -54,9 +53,11 @@ import com.taover.easyexcel.analysis.v03.handlers.NoteRecordHandler; | ||
| 54 | import com.taover.easyexcel.analysis.v03.handlers.NumberRecordHandler; | 53 | import com.taover.easyexcel.analysis.v03.handlers.NumberRecordHandler; |
| 55 | import com.taover.easyexcel.analysis.v03.handlers.ObjRecordHandler; | 54 | import com.taover.easyexcel.analysis.v03.handlers.ObjRecordHandler; |
| 56 | import com.taover.easyexcel.analysis.v03.handlers.RkRecordHandler; | 55 | import com.taover.easyexcel.analysis.v03.handlers.RkRecordHandler; |
| 56 | +import com.taover.easyexcel.analysis.v03.handlers.RowRecordHandler; | ||
| 57 | import com.taover.easyexcel.analysis.v03.handlers.SstRecordHandler; | 57 | import com.taover.easyexcel.analysis.v03.handlers.SstRecordHandler; |
| 58 | import com.taover.easyexcel.analysis.v03.handlers.StringRecordHandler; | 58 | import com.taover.easyexcel.analysis.v03.handlers.StringRecordHandler; |
| 59 | import com.taover.easyexcel.analysis.v03.handlers.TextObjectRecordHandler; | 59 | import com.taover.easyexcel.analysis.v03.handlers.TextObjectRecordHandler; |
| 60 | +import com.taover.easyexcel.analysis.v03.handlers.WindowOneRecordHandler; | ||
| 60 | import com.taover.easyexcel.context.xls.XlsReadContext; | 61 | import com.taover.easyexcel.context.xls.XlsReadContext; |
| 61 | import com.taover.easyexcel.exception.ExcelAnalysisException; | 62 | import com.taover.easyexcel.exception.ExcelAnalysisException; |
| 62 | import com.taover.easyexcel.exception.ExcelAnalysisStopException; | 63 | import com.taover.easyexcel.exception.ExcelAnalysisStopException; |
| @@ -83,9 +84,6 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { | @@ -83,9 +84,6 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { | ||
| 83 | private static final short DUMMY_RECORD_SID = -1; | 84 | private static final short DUMMY_RECORD_SID = -1; |
| 84 | private XlsReadContext xlsReadContext; | 85 | private XlsReadContext xlsReadContext; |
| 85 | private static final Map<Short, XlsRecordHandler> XLS_RECORD_HANDLER_MAP = new HashMap<Short, XlsRecordHandler>(32); | 86 | private static final Map<Short, XlsRecordHandler> XLS_RECORD_HANDLER_MAP = new HashMap<Short, XlsRecordHandler>(32); |
| 86 | - List<Integer> skipCellRowIndexList = new ArrayList<Integer>(); | ||
| 87 | - private Integer activeSheetIndex = null; | ||
| 88 | - private int currSheetIndex = -1; | ||
| 89 | 87 | ||
| 90 | static { | 88 | static { |
| 91 | XLS_RECORD_HANDLER_MAP.put(BlankRecord.sid, new BlankRecordHandler()); | 89 | XLS_RECORD_HANDLER_MAP.put(BlankRecord.sid, new BlankRecordHandler()); |
| @@ -107,6 +105,8 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { | @@ -107,6 +105,8 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { | ||
| 107 | XLS_RECORD_HANDLER_MAP.put(SSTRecord.sid, new SstRecordHandler()); | 105 | XLS_RECORD_HANDLER_MAP.put(SSTRecord.sid, new SstRecordHandler()); |
| 108 | XLS_RECORD_HANDLER_MAP.put(StringRecord.sid, new StringRecordHandler()); | 106 | XLS_RECORD_HANDLER_MAP.put(StringRecord.sid, new StringRecordHandler()); |
| 109 | XLS_RECORD_HANDLER_MAP.put(TextObjectRecord.sid, new TextObjectRecordHandler()); | 107 | XLS_RECORD_HANDLER_MAP.put(TextObjectRecord.sid, new TextObjectRecordHandler()); |
| 108 | + XLS_RECORD_HANDLER_MAP.put(WindowOneRecord.sid, new WindowOneRecordHandler()); | ||
| 109 | + XLS_RECORD_HANDLER_MAP.put(RowRecord.sid, new RowRecordHandler()); | ||
| 110 | } | 110 | } |
| 111 | 111 | ||
| 112 | public XlsSaxAnalyser(XlsReadContext xlsReadContext) { | 112 | public XlsSaxAnalyser(XlsReadContext xlsReadContext) { |
| @@ -139,6 +139,7 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { | @@ -139,6 +139,7 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { | ||
| 139 | HSSFEventFactory factory = new HSSFEventFactory(); | 139 | HSSFEventFactory factory = new HSSFEventFactory(); |
| 140 | HSSFRequest request = new HSSFRequest(); | 140 | HSSFRequest request = new HSSFRequest(); |
| 141 | request.addListenerForAllRecords(xlsReadWorkbookHolder.getFormatTrackingHSSFListener()); | 141 | request.addListenerForAllRecords(xlsReadWorkbookHolder.getFormatTrackingHSSFListener()); |
| 142 | + | ||
| 142 | try { | 143 | try { |
| 143 | factory.processWorkbookEvents(request, xlsReadWorkbookHolder.getPoifsFileSystem()); | 144 | factory.processWorkbookEvents(request, xlsReadWorkbookHolder.getPoifsFileSystem()); |
| 144 | } catch (IOException e) { | 145 | } catch (IOException e) { |
| @@ -148,22 +149,11 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { | @@ -148,22 +149,11 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { | ||
| 148 | 149 | ||
| 149 | @Override | 150 | @Override |
| 150 | public void processRecord(Record record) { | 151 | public void processRecord(Record record) { |
| 151 | - System.out.println(record); | ||
| 152 | - | ||
| 153 | - //flush global data | ||
| 154 | - this.initGlobalXlsData(xlsReadContext, record); | ||
| 155 | - | ||
| 156 | - //check whether skip | ||
| 157 | - if(this.needSkip(xlsReadContext, record)) { | ||
| 158 | - return; | ||
| 159 | - } | ||
| 160 | - | ||
| 161 | XlsRecordHandler handler = XLS_RECORD_HANDLER_MAP.get(record.getSid()); | 152 | XlsRecordHandler handler = XLS_RECORD_HANDLER_MAP.get(record.getSid()); |
| 162 | if (handler == null) { | 153 | if (handler == null) { |
| 163 | return; | 154 | return; |
| 164 | } | 155 | } |
| 165 | - boolean ignoreRecord = (handler instanceof IgnorableXlsRecordHandler) | ||
| 166 | - && xlsReadContext.xlsReadSheetHolder() != null && xlsReadContext.xlsReadWorkbookHolder().getIgnoreRecord(); | 156 | + boolean ignoreRecord = (handler instanceof IgnorableXlsRecordHandler) && xlsReadContext.xlsReadWorkbookHolder().getIgnoreRecord(); |
| 167 | if (ignoreRecord) { | 157 | if (ignoreRecord) { |
| 168 | // No need to read the current sheet | 158 | // No need to read the current sheet |
| 169 | return; | 159 | return; |
| @@ -173,40 +163,4 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { | @@ -173,40 +163,4 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { | ||
| 173 | } | 163 | } |
| 174 | handler.processRecord(xlsReadContext, record); | 164 | handler.processRecord(xlsReadContext, record); |
| 175 | } | 165 | } |
| 176 | - | ||
| 177 | - private void initGlobalXlsData(XlsReadContext xlsReadContext2, Record record) { | ||
| 178 | - if(record.getSid() == EOFRecord.sid) { | ||
| 179 | - this.skipCellRowIndexList.clear(); | ||
| 180 | - ++this.currSheetIndex; | ||
| 181 | - } else if(record.getSid() == WindowOneRecord.sid) { | ||
| 182 | - WindowOneRecord window = (WindowOneRecord)record; | ||
| 183 | - this.activeSheetIndex = window.getActiveSheetIndex(); | ||
| 184 | - } | ||
| 185 | - } | ||
| 186 | - | ||
| 187 | - private boolean needSkip(XlsReadContext xlsReadContext, Record record) { | ||
| 188 | - if(record.getSid() == RowRecord.sid) { | ||
| 189 | - RowRecord rowRec = (RowRecord) record; | ||
| 190 | - Boolean readHiddenRow = xlsReadContext.xlsReadWorkbookHolder().getReadHiddenRow(); | ||
| 191 | - if(readHiddenRow != null | ||
| 192 | - && !readHiddenRow | ||
| 193 | - && rowRec.getZeroHeight()) { | ||
| 194 | - skipCellRowIndexList.add(rowRec.getRowNumber()); | ||
| 195 | - return true; | ||
| 196 | - } | ||
| 197 | - }else if(record instanceof CellRecord) { | ||
| 198 | - CellRecord cellRec = (CellRecord)record; | ||
| 199 | - Boolean justReadActiveSheet = xlsReadContext.xlsReadWorkbookHolder().getReadJustSelected(); | ||
| 200 | - if(justReadActiveSheet != null | ||
| 201 | - && justReadActiveSheet | ||
| 202 | - && this.activeSheetIndex != null | ||
| 203 | - && this.currSheetIndex != this.activeSheetIndex) { | ||
| 204 | - return true; | ||
| 205 | - }else if(skipCellRowIndexList.contains(cellRec.getRow())) { | ||
| 206 | - return true; | ||
| 207 | - } | ||
| 208 | - } | ||
| 209 | - | ||
| 210 | - return false; | ||
| 211 | - } | ||
| 212 | } | 166 | } |
src/main/java/com/taover/easyexcel/analysis/v03/handlers/AbstractXlsRecordHandler.java
| 1 | package com.taover.easyexcel.analysis.v03.handlers; | 1 | package com.taover.easyexcel.analysis.v03.handlers; |
| 2 | 2 | ||
| 3 | +import org.apache.poi.hssf.record.CellRecord; | ||
| 3 | import org.apache.poi.hssf.record.Record; | 4 | import org.apache.poi.hssf.record.Record; |
| 4 | 5 | ||
| 5 | import com.taover.easyexcel.analysis.v03.XlsRecordHandler; | 6 | import com.taover.easyexcel.analysis.v03.XlsRecordHandler; |
| @@ -13,6 +14,23 @@ import com.taover.easyexcel.context.xls.XlsReadContext; | @@ -13,6 +14,23 @@ import com.taover.easyexcel.context.xls.XlsReadContext; | ||
| 13 | public abstract class AbstractXlsRecordHandler implements XlsRecordHandler { | 14 | public abstract class AbstractXlsRecordHandler implements XlsRecordHandler { |
| 14 | @Override | 15 | @Override |
| 15 | public boolean support(XlsReadContext xlsReadContext, Record record) { | 16 | public boolean support(XlsReadContext xlsReadContext, Record record) { |
| 16 | - return true; | 17 | + if(record instanceof CellRecord) { |
| 18 | + Boolean readHiddenRow = xlsReadContext.xlsReadWorkbookHolder().getReadHiddenRow(); | ||
| 19 | + Boolean justReadActiveSheet = xlsReadContext.xlsReadWorkbookHolder().getReadJustSelected(); | ||
| 20 | + if(readHiddenRow == null) { | ||
| 21 | + readHiddenRow = true; | ||
| 22 | + } | ||
| 23 | + if(justReadActiveSheet == null) { | ||
| 24 | + justReadActiveSheet = false; | ||
| 25 | + } | ||
| 26 | + CellRecord cellRec = (CellRecord)record; | ||
| 27 | + if(xlsReadContext.xlsReadSheetHolder() == null) { | ||
| 28 | + return false; | ||
| 29 | + } | ||
| 30 | + if(!readHiddenRow && xlsReadContext.xlsReadSheetHolder().getHiddenRowIndexList().contains(cellRec.getRow())) { | ||
| 31 | + return false; | ||
| 32 | + } | ||
| 33 | + } | ||
| 34 | + return true; | ||
| 17 | } | 35 | } |
| 18 | } | 36 | } |
src/main/java/com/taover/easyexcel/analysis/v03/handlers/BofRecordHandler.java
| @@ -10,8 +10,10 @@ import org.apache.poi.hssf.record.Record; | @@ -10,8 +10,10 @@ import org.apache.poi.hssf.record.Record; | ||
| 10 | import com.taover.easyexcel.context.xls.XlsReadContext; | 10 | import com.taover.easyexcel.context.xls.XlsReadContext; |
| 11 | import com.taover.easyexcel.exception.ExcelAnalysisStopException; | 11 | import com.taover.easyexcel.exception.ExcelAnalysisStopException; |
| 12 | import com.taover.easyexcel.read.metadata.ReadSheet; | 12 | import com.taover.easyexcel.read.metadata.ReadSheet; |
| 13 | +import com.taover.easyexcel.read.metadata.holder.ReadWorkbookHolder; | ||
| 13 | import com.taover.easyexcel.read.metadata.holder.xls.XlsReadWorkbookHolder; | 14 | import com.taover.easyexcel.read.metadata.holder.xls.XlsReadWorkbookHolder; |
| 14 | import com.taover.easyexcel.util.SheetUtils; | 15 | import com.taover.easyexcel.util.SheetUtils; |
| 16 | +import com.taover.easyexcel.util.StringUtils; | ||
| 15 | 17 | ||
| 16 | /** | 18 | /** |
| 17 | * Record handler | 19 | * Record handler |
| @@ -42,7 +44,7 @@ public class BofRecordHandler extends AbstractXlsRecordHandler { | @@ -42,7 +44,7 @@ public class BofRecordHandler extends AbstractXlsRecordHandler { | ||
| 42 | ReadSheet actualReadSheet = xlsReadWorkbookHolder.getActualSheetDataList().get(readSheetIndex); | 44 | ReadSheet actualReadSheet = xlsReadWorkbookHolder.getActualSheetDataList().get(readSheetIndex); |
| 43 | assert actualReadSheet != null : "Can't find the sheet."; | 45 | assert actualReadSheet != null : "Can't find the sheet."; |
| 44 | // Copy the parameter to the current sheet | 46 | // Copy the parameter to the current sheet |
| 45 | - ReadSheet readSheet = SheetUtils.match(actualReadSheet, xlsReadContext); | 47 | + ReadSheet readSheet = this.filterReadSheet(actualReadSheet, xlsReadContext); |
| 46 | if (readSheet != null) { | 48 | if (readSheet != null) { |
| 47 | xlsReadContext.currentSheet(readSheet); | 49 | xlsReadContext.currentSheet(readSheet); |
| 48 | xlsReadContext.xlsReadWorkbookHolder().setIgnoreRecord(Boolean.FALSE); | 50 | xlsReadContext.xlsReadWorkbookHolder().setIgnoreRecord(Boolean.FALSE); |
| @@ -53,7 +55,59 @@ public class BofRecordHandler extends AbstractXlsRecordHandler { | @@ -53,7 +55,59 @@ public class BofRecordHandler extends AbstractXlsRecordHandler { | ||
| 53 | xlsReadWorkbookHolder.setReadSheetIndex(xlsReadWorkbookHolder.getReadSheetIndex() + 1); | 55 | xlsReadWorkbookHolder.setReadSheetIndex(xlsReadWorkbookHolder.getReadSheetIndex() + 1); |
| 54 | } | 56 | } |
| 55 | 57 | ||
| 56 | - private void initReadSheetDataList(XlsReadWorkbookHolder xlsReadWorkbookHolder) { | 58 | + private ReadSheet filterReadSheet(ReadSheet readSheet, XlsReadContext analysisContext) { |
| 59 | + ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder(); | ||
| 60 | + //hidden sheet not read | ||
| 61 | + if(analysisContext.xlsReadWorkbookHolder().getHiddenBoundSheetIndexList().contains(readSheet.getSheetNo())) { | ||
| 62 | + return null; | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + //read all and not read just selected | ||
| 66 | + if (readWorkbookHolder.getReadAll()) { | ||
| 67 | + return readSheet; | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + //read selected sheet | ||
| 71 | + if(readWorkbookHolder.getReadJustSelected()) { | ||
| 72 | + if(readSheet.getSheetNo() == analysisContext.xlsReadWorkbookHolder().getActiveSheetIndex()) { | ||
| 73 | + return readSheet; | ||
| 74 | + }else { | ||
| 75 | + return null; | ||
| 76 | + } | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + //read by sheet no | ||
| 80 | + for (ReadSheet parameterReadSheet : readWorkbookHolder.getParameterSheetDataList()) { | ||
| 81 | + //remove null or hidden sheet | ||
| 82 | + if (parameterReadSheet == null) { | ||
| 83 | + continue; | ||
| 84 | + } | ||
| 85 | + if (parameterReadSheet.getSheetNo() == null && parameterReadSheet.getSheetName() == null) { | ||
| 86 | + parameterReadSheet.setSheetNo(0); | ||
| 87 | + } | ||
| 88 | + boolean match = (parameterReadSheet.getSheetNo() != null | ||
| 89 | + && parameterReadSheet.getSheetNo().equals(readSheet.getSheetNo())); | ||
| 90 | + if (!match) { | ||
| 91 | + String parameterSheetName = parameterReadSheet.getSheetName(); | ||
| 92 | + if (!StringUtils.isEmpty(parameterSheetName)) { | ||
| 93 | + boolean autoTrim = (parameterReadSheet.getAutoTrim() != null && parameterReadSheet.getAutoTrim()) | ||
| 94 | + || (parameterReadSheet.getAutoTrim() == null | ||
| 95 | + && analysisContext.readWorkbookHolder().getGlobalConfiguration().getAutoTrim()); | ||
| 96 | + if (autoTrim) { | ||
| 97 | + parameterSheetName = parameterSheetName.trim(); | ||
| 98 | + } | ||
| 99 | + match = parameterSheetName.equals(readSheet.getSheetName()); | ||
| 100 | + } | ||
| 101 | + } | ||
| 102 | + if (match) { | ||
| 103 | + readSheet.copyBasicParameter(parameterReadSheet); | ||
| 104 | + return readSheet; | ||
| 105 | + } | ||
| 106 | + } | ||
| 107 | + return null; | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + private void initReadSheetDataList(XlsReadWorkbookHolder xlsReadWorkbookHolder) { | ||
| 57 | if (xlsReadWorkbookHolder.getActualSheetDataList() != null) { | 111 | if (xlsReadWorkbookHolder.getActualSheetDataList() != null) { |
| 58 | return; | 112 | return; |
| 59 | } | 113 | } |
| @@ -63,7 +117,13 @@ public class BofRecordHandler extends AbstractXlsRecordHandler { | @@ -63,7 +117,13 @@ public class BofRecordHandler extends AbstractXlsRecordHandler { | ||
| 63 | for (int i = 0; i < boundSheetRecords.length; i++) { | 117 | for (int i = 0; i < boundSheetRecords.length; i++) { |
| 64 | BoundSheetRecord boundSheetRecord = boundSheetRecords[i]; | 118 | BoundSheetRecord boundSheetRecord = boundSheetRecords[i]; |
| 65 | ReadSheet readSheet = new ReadSheet(i, boundSheetRecord.getSheetname()); | 119 | ReadSheet readSheet = new ReadSheet(i, boundSheetRecord.getSheetname()); |
| 66 | - readSheetDataList.add(readSheet); | 120 | + if(boundSheetRecord.isHidden() || boundSheetRecord.isVeryHidden()) { |
| 121 | + readSheet.setSheetHidden(true); | ||
| 122 | + xlsReadWorkbookHolder.getHiddenBoundSheetIndexList().add(i); | ||
| 123 | + }else { | ||
| 124 | + readSheet.setSheetHidden(false); | ||
| 125 | + } | ||
| 126 | + readSheetDataList.add(readSheet); | ||
| 67 | } | 127 | } |
| 68 | xlsReadWorkbookHolder.setActualSheetDataList(readSheetDataList); | 128 | xlsReadWorkbookHolder.setActualSheetDataList(readSheetDataList); |
| 69 | // Just need to get the list of sheets | 129 | // Just need to get the list of sheets |
src/main/java/com/taover/easyexcel/analysis/v03/handlers/RowRecordHandler.java
0 → 100644
| @@ -0,0 +1,26 @@ | @@ -0,0 +1,26 @@ | ||
| 1 | +package com.taover.easyexcel.analysis.v03.handlers; | ||
| 2 | + | ||
| 3 | +import org.apache.poi.hssf.record.Record; | ||
| 4 | +import org.apache.poi.hssf.record.RowRecord; | ||
| 5 | +import org.slf4j.Logger; | ||
| 6 | +import org.slf4j.LoggerFactory; | ||
| 7 | + | ||
| 8 | +import com.taover.easyexcel.context.xls.XlsReadContext; | ||
| 9 | + | ||
| 10 | +/** | ||
| 11 | + * Record handler | ||
| 12 | + * | ||
| 13 | + * @author Dan Zheng | ||
| 14 | + */ | ||
| 15 | +public class RowRecordHandler extends AbstractXlsRecordHandler { | ||
| 16 | + private static final Logger LOGGER = LoggerFactory.getLogger(RowRecordHandler.class); | ||
| 17 | + | ||
| 18 | + @Override | ||
| 19 | + public void processRecord(XlsReadContext xlsReadContext, Record record) { | ||
| 20 | + RowRecord rowRecord = (RowRecord)record; | ||
| 21 | + if(rowRecord.getZeroHeight() && | ||
| 22 | + xlsReadContext.xlsReadSheetHolder() != null) { | ||
| 23 | + xlsReadContext.xlsReadSheetHolder().getHiddenRowIndexList().add(rowRecord.getRowNumber()); | ||
| 24 | + } | ||
| 25 | + } | ||
| 26 | +} |
src/main/java/com/taover/easyexcel/analysis/v03/handlers/WindowOneRecordHandler.java
0 → 100644
| @@ -0,0 +1,23 @@ | @@ -0,0 +1,23 @@ | ||
| 1 | +package com.taover.easyexcel.analysis.v03.handlers; | ||
| 2 | + | ||
| 3 | +import org.apache.poi.hssf.record.Record; | ||
| 4 | +import org.apache.poi.hssf.record.WindowOneRecord; | ||
| 5 | +import org.slf4j.Logger; | ||
| 6 | +import org.slf4j.LoggerFactory; | ||
| 7 | + | ||
| 8 | +import com.taover.easyexcel.context.xls.XlsReadContext; | ||
| 9 | + | ||
| 10 | +/** | ||
| 11 | + * Record handler | ||
| 12 | + * | ||
| 13 | + * @author Dan Zheng | ||
| 14 | + */ | ||
| 15 | +public class WindowOneRecordHandler extends AbstractXlsRecordHandler { | ||
| 16 | + private static final Logger LOGGER = LoggerFactory.getLogger(WindowOneRecordHandler.class); | ||
| 17 | + | ||
| 18 | + @Override | ||
| 19 | + public void processRecord(XlsReadContext xlsReadContext, Record record) { | ||
| 20 | + WindowOneRecord windowOne = (WindowOneRecord)record; | ||
| 21 | + xlsReadContext.xlsReadWorkbookHolder().setActiveSheetIndex(windowOne.getActiveSheetIndex()); | ||
| 22 | + } | ||
| 23 | +} |
src/main/java/com/taover/easyexcel/analysis/v07/handlers/sax/XlsxRowHandler.java
| @@ -53,9 +53,9 @@ public class XlsxRowHandler extends DefaultHandler { | @@ -53,9 +53,9 @@ public class XlsxRowHandler extends DefaultHandler { | ||
| 53 | RowTagHandler rowTagHandler = new RowTagHandler(); | 53 | RowTagHandler rowTagHandler = new RowTagHandler(); |
| 54 | XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.ROW_TAG, rowTagHandler); | 54 | XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.ROW_TAG, rowTagHandler); |
| 55 | XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_ROW_TAG, rowTagHandler); | 55 | XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_ROW_TAG, rowTagHandler); |
| 56 | - SheetViewHandler sheetViewHandler = new SheetViewHandler(); | ||
| 57 | - XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.SHEET_VIEW_TAG, sheetViewHandler); | ||
| 58 | - XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_SHEET_VIEW_TAG, sheetViewHandler); | 56 | +// SheetViewHandler sheetViewHandler = new SheetViewHandler(); |
| 57 | +// XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.SHEET_VIEW_TAG, sheetViewHandler); | ||
| 58 | +// XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_SHEET_VIEW_TAG, sheetViewHandler); | ||
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | public XlsxRowHandler(XlsxReadContext xlsxReadContext) { | 61 | public XlsxRowHandler(XlsxReadContext xlsxReadContext) { |
src/main/java/com/taover/easyexcel/analysis/v07/workbook/WorkbookAnalyserImpl.java
| @@ -30,9 +30,14 @@ public class WorkbookAnalyserImpl implements WorkbookAnalyser { | @@ -30,9 +30,14 @@ public class WorkbookAnalyserImpl implements WorkbookAnalyser { | ||
| 30 | public WorkbookAnalyserImpl(InputStream workbookInputStream) throws SAXException, IOException, ParserConfigurationException { | 30 | public WorkbookAnalyserImpl(InputStream workbookInputStream) throws SAXException, IOException, ParserConfigurationException { |
| 31 | Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(workbookInputStream); | 31 | Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(workbookInputStream); |
| 32 | NodeList nList = doc.getElementsByTagName(TAG_NAME_WORKBOOK); | 32 | NodeList nList = doc.getElementsByTagName(TAG_NAME_WORKBOOK); |
| 33 | - Node workbookNode = nList.item(0); | ||
| 34 | - this.initSheetList(this.getSheetsDoc(workbookNode.getOwnerDocument())); | ||
| 35 | - this.initActiveTabIndex(this.getWorkbookViewDoc(workbookNode.getOwnerDocument())); | 33 | + Node workbookNode = nList.item(0); |
| 34 | + if(workbookNode != null) { | ||
| 35 | + this.initSheetList(this.getSheetsDoc(workbookNode.getOwnerDocument())); | ||
| 36 | + this.initActiveTabIndex(this.getWorkbookViewDoc(workbookNode.getOwnerDocument())); | ||
| 37 | + }else { | ||
| 38 | + this.activeTabIndex = 0; | ||
| 39 | + this.sheetList = new ArrayList<WorkbookSheet>(); | ||
| 40 | + } | ||
| 36 | } | 41 | } |
| 37 | 42 | ||
| 38 | private void initSheetList(List<Node> sheetsDoc) { | 43 | private void initSheetList(List<Node> sheetsDoc) { |
| @@ -81,10 +86,13 @@ public class WorkbookAnalyserImpl implements WorkbookAnalyser { | @@ -81,10 +86,13 @@ public class WorkbookAnalyserImpl implements WorkbookAnalyser { | ||
| 81 | } | 86 | } |
| 82 | 87 | ||
| 83 | private List<Node> getSheetsDoc(Document workbook) { | 88 | private List<Node> getSheetsDoc(Document workbook) { |
| 89 | + List<Node> result = new ArrayList<Node>(); | ||
| 90 | + if(workbook == null) { | ||
| 91 | + return result; | ||
| 92 | + } | ||
| 84 | NodeList sheetsNodeList = workbook.getElementsByTagName(TAG_NAME_SHEETS); | 93 | NodeList sheetsNodeList = workbook.getElementsByTagName(TAG_NAME_SHEETS); |
| 85 | Node sheetsNode = sheetsNodeList.item(0); | 94 | Node sheetsNode = sheetsNodeList.item(0); |
| 86 | - NodeList sheetNodeList = sheetsNode.getChildNodes(); | ||
| 87 | - List<Node> result = new ArrayList<Node>(); | 95 | + NodeList sheetNodeList = sheetsNode.getChildNodes(); |
| 88 | for(int i=0; i<sheetNodeList.getLength(); ++i) { | 96 | for(int i=0; i<sheetNodeList.getLength(); ++i) { |
| 89 | result.add(sheetNodeList.item(i)); | 97 | result.add(sheetNodeList.item(i)); |
| 90 | } | 98 | } |
| @@ -92,10 +100,13 @@ public class WorkbookAnalyserImpl implements WorkbookAnalyser { | @@ -92,10 +100,13 @@ public class WorkbookAnalyserImpl implements WorkbookAnalyser { | ||
| 92 | } | 100 | } |
| 93 | 101 | ||
| 94 | private List<Node> getWorkbookViewDoc(Document workbook) { | 102 | private List<Node> getWorkbookViewDoc(Document workbook) { |
| 103 | + List<Node> result = new ArrayList<Node>(); | ||
| 104 | + if(workbook == null) { | ||
| 105 | + return result; | ||
| 106 | + } | ||
| 95 | NodeList sheetsNodeList = workbook.getElementsByTagName(TAG_NAME_BOOKVIEWS); | 107 | NodeList sheetsNodeList = workbook.getElementsByTagName(TAG_NAME_BOOKVIEWS); |
| 96 | Node sheetsNode = sheetsNodeList.item(0); | 108 | Node sheetsNode = sheetsNodeList.item(0); |
| 97 | - NodeList sheetNodeList = sheetsNode.getChildNodes(); | ||
| 98 | - List<Node> result = new ArrayList<Node>(); | 109 | + NodeList sheetNodeList = sheetsNode.getChildNodes(); |
| 99 | for(int i=0; i<sheetNodeList.getLength(); ++i) { | 110 | for(int i=0; i<sheetNodeList.getLength(); ++i) { |
| 100 | result.add(sheetNodeList.item(i)); | 111 | result.add(sheetNodeList.item(i)); |
| 101 | } | 112 | } |
src/main/java/com/taover/easyexcel/read/builder/ExcelReaderBuilder.java
| @@ -217,7 +217,7 @@ public class ExcelReaderBuilder extends AbstractExcelReaderParameterBuilder<Exce | @@ -217,7 +217,7 @@ public class ExcelReaderBuilder extends AbstractExcelReaderParameterBuilder<Exce | ||
| 217 | * | 217 | * |
| 218 | * @return | 218 | * @return |
| 219 | */ | 219 | */ |
| 220 | - public <T> List<T> doReadAllSync() { | 220 | + public <T> List<T> doReadAllSync() { |
| 221 | SyncReadListener syncReadListener = new SyncReadListener(); | 221 | SyncReadListener syncReadListener = new SyncReadListener(); |
| 222 | registerReadListener(syncReadListener); | 222 | registerReadListener(syncReadListener); |
| 223 | ExcelReader excelReader = build(); | 223 | ExcelReader excelReader = build(); |
src/main/java/com/taover/easyexcel/read/metadata/holder/xls/XlsReadSheetHolder.java
| 1 | package com.taover.easyexcel.read.metadata.holder.xls; | 1 | package com.taover.easyexcel.read.metadata.holder.xls; |
| 2 | 2 | ||
| 3 | +import java.util.ArrayList; | ||
| 3 | import java.util.HashMap; | 4 | import java.util.HashMap; |
| 5 | +import java.util.List; | ||
| 4 | import java.util.Map; | 6 | import java.util.Map; |
| 5 | 7 | ||
| 6 | import com.taover.easyexcel.enums.RowTypeEnum; | 8 | import com.taover.easyexcel.enums.RowTypeEnum; |
| @@ -26,11 +28,14 @@ public class XlsReadSheetHolder extends ReadSheetHolder { | @@ -26,11 +28,14 @@ public class XlsReadSheetHolder extends ReadSheetHolder { | ||
| 26 | * Temp object index. | 28 | * Temp object index. |
| 27 | */ | 29 | */ |
| 28 | private Map<Integer, String> objectCacheMap; | 30 | private Map<Integer, String> objectCacheMap; |
| 31 | + | ||
| 32 | + List<Integer> hiddenRowIndexList; | ||
| 29 | 33 | ||
| 30 | public XlsReadSheetHolder(ReadSheet readSheet, ReadWorkbookHolder readWorkbookHolder) { | 34 | public XlsReadSheetHolder(ReadSheet readSheet, ReadWorkbookHolder readWorkbookHolder) { |
| 31 | super(readSheet, readWorkbookHolder); | 35 | super(readSheet, readWorkbookHolder); |
| 32 | tempRowType = RowTypeEnum.EMPTY; | 36 | tempRowType = RowTypeEnum.EMPTY; |
| 33 | objectCacheMap = new HashMap<Integer, String>(16); | 37 | objectCacheMap = new HashMap<Integer, String>(16); |
| 38 | + hiddenRowIndexList = new ArrayList<Integer>(16); | ||
| 34 | } | 39 | } |
| 35 | 40 | ||
| 36 | public RowTypeEnum getTempRowType() { | 41 | public RowTypeEnum getTempRowType() { |
| @@ -57,4 +62,12 @@ public class XlsReadSheetHolder extends ReadSheetHolder { | @@ -57,4 +62,12 @@ public class XlsReadSheetHolder extends ReadSheetHolder { | ||
| 57 | public void setObjectCacheMap(Map<Integer, String> objectCacheMap) { | 62 | public void setObjectCacheMap(Map<Integer, String> objectCacheMap) { |
| 58 | this.objectCacheMap = objectCacheMap; | 63 | this.objectCacheMap = objectCacheMap; |
| 59 | } | 64 | } |
| 65 | + | ||
| 66 | + public List<Integer> getHiddenRowIndexList() { | ||
| 67 | + return hiddenRowIndexList; | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + public void setHiddenRowIndexList(List<Integer> hiddenRowIndexList) { | ||
| 71 | + this.hiddenRowIndexList = hiddenRowIndexList; | ||
| 72 | + } | ||
| 60 | } | 73 | } |
src/main/java/com/taover/easyexcel/read/metadata/holder/xls/XlsReadWorkbookHolder.java
| @@ -46,6 +46,9 @@ public class XlsReadWorkbookHolder extends ReadWorkbookHolder { | @@ -46,6 +46,9 @@ public class XlsReadWorkbookHolder extends ReadWorkbookHolder { | ||
| 46 | * Ignore record. | 46 | * Ignore record. |
| 47 | */ | 47 | */ |
| 48 | private Boolean ignoreRecord; | 48 | private Boolean ignoreRecord; |
| 49 | + | ||
| 50 | + private int activeSheetIndex = 0; | ||
| 51 | + private List<Integer> hiddenBoundSheetIndexList = new ArrayList<Integer>(); | ||
| 49 | 52 | ||
| 50 | public XlsReadWorkbookHolder(ReadWorkbook readWorkbook) { | 53 | public XlsReadWorkbookHolder(ReadWorkbook readWorkbook) { |
| 51 | super(readWorkbook); | 54 | super(readWorkbook); |
| @@ -113,4 +116,20 @@ public class XlsReadWorkbookHolder extends ReadWorkbookHolder { | @@ -113,4 +116,20 @@ public class XlsReadWorkbookHolder extends ReadWorkbookHolder { | ||
| 113 | public void setIgnoreRecord(Boolean ignoreRecord) { | 116 | public void setIgnoreRecord(Boolean ignoreRecord) { |
| 114 | this.ignoreRecord = ignoreRecord; | 117 | this.ignoreRecord = ignoreRecord; |
| 115 | } | 118 | } |
| 119 | + | ||
| 120 | + public int getActiveSheetIndex() { | ||
| 121 | + return activeSheetIndex; | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + public void setActiveSheetIndex(int activeSheetIndex) { | ||
| 125 | + this.activeSheetIndex = activeSheetIndex; | ||
| 126 | + } | ||
| 127 | + | ||
| 128 | + public List<Integer> getHiddenBoundSheetIndexList() { | ||
| 129 | + return hiddenBoundSheetIndexList; | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + public void setHiddenBoundSheetIndexList(List<Integer> hiddenBoundSheetIndexList) { | ||
| 133 | + this.hiddenBoundSheetIndexList = hiddenBoundSheetIndexList; | ||
| 134 | + } | ||
| 116 | } | 135 | } |
src/main/java/com/taover/easyexcel/util/SheetUtils.java
| @@ -28,27 +28,10 @@ public class SheetUtils { | @@ -28,27 +28,10 @@ public class SheetUtils { | ||
| 28 | public static ReadSheet match(ReadSheet readSheet, AnalysisContext analysisContext) { | 28 | public static ReadSheet match(ReadSheet readSheet, AnalysisContext analysisContext) { |
| 29 | ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder(); | 29 | ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder(); |
| 30 | //read all and not read just selected | 30 | //read all and not read just selected |
| 31 | - if (readWorkbookHolder.getReadAll()) { | 31 | + if (readWorkbookHolder.getReadAll() || readWorkbookHolder.getReadJustSelected()) { |
| 32 | return readSheet; | 32 | return readSheet; |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | - if(readWorkbookHolder.getParameterSheetDataList() == null) { | ||
| 36 | - return null; | ||
| 37 | - } | ||
| 38 | - | ||
| 39 | - //read just selected | ||
| 40 | - if(readWorkbookHolder.getReadJustSelected()) { | ||
| 41 | - for (ReadSheet parameterReadSheet : readWorkbookHolder.getParameterSheetDataList()) { | ||
| 42 | - if (parameterReadSheet == null) { | ||
| 43 | - continue; | ||
| 44 | - } | ||
| 45 | - if(parameterReadSheet.getSheetSelected() != null && parameterReadSheet.getSheetSelected()) { | ||
| 46 | - readSheet.copyBasicParameter(parameterReadSheet); | ||
| 47 | - return readSheet; | ||
| 48 | - } | ||
| 49 | - } | ||
| 50 | - } | ||
| 51 | - | ||
| 52 | //read by sheet no | 35 | //read by sheet no |
| 53 | for (ReadSheet parameterReadSheet : readWorkbookHolder.getParameterSheetDataList()) { | 36 | for (ReadSheet parameterReadSheet : readWorkbookHolder.getParameterSheetDataList()) { |
| 54 | //remove null or hidden sheet | 37 | //remove null or hidden sheet |
src/test/java/com/taover/easyexcel/test/demo/read/ReadTest.java
| @@ -252,7 +252,7 @@ public class ReadTest { | @@ -252,7 +252,7 @@ public class ReadTest { | ||
| 252 | // } | 252 | // } |
| 253 | 253 | ||
| 254 | // 这里 也可以不指定class,返回一个list,然后读取第一个sheet 同步读取会自动finish | 254 | // 这里 也可以不指定class,返回一个list,然后读取第一个sheet 同步读取会自动finish |
| 255 | - List<Map<Integer, String>> listMap = EasyExcel.read("D://debug//hiddensheet//demo.xls").headRowNumber(0).readHiddenRow(false).doReadSelectedSync(); | 255 | + List<Map<Integer, String>> listMap = EasyExcel.read("D://debug//hiddensheet//demo.xlsx").headRowNumber(0).readHiddenRow(false).doReadSelectedSync(); |
| 256 | for (Map<Integer, String> data : listMap) { | 256 | for (Map<Integer, String> data : listMap) { |
| 257 | // 返回每条数据的键值对 表示所在的列 和所在列的值 | 257 | // 返回每条数据的键值对 表示所在的列 和所在列的值 |
| 258 | LOGGER.info("读取到数据:{}", JSON.toJSONString(data)); | 258 | LOGGER.info("读取到数据:{}", JSON.toJSONString(data)); |
| @@ -274,17 +274,18 @@ public class ReadTest { | @@ -274,17 +274,18 @@ public class ReadTest { | ||
| 274 | } | 274 | } |
| 275 | 275 | ||
| 276 | public void readTestSynchronousRead() { | 276 | public void readTestSynchronousRead() { |
| 277 | - String dirStr = "D://debug//suffixwrong//"; | 277 | + String dirStr = "D:\\debug\\readtest\\"; |
| 278 | File dDir = new File(dirStr); | 278 | File dDir = new File(dirStr); |
| 279 | String[] sonFileNameArr = dDir.list(); | 279 | String[] sonFileNameArr = dDir.list(); |
| 280 | for(String item: sonFileNameArr) { | 280 | for(String item: sonFileNameArr) { |
| 281 | try { | 281 | try { |
| 282 | LOGGER.info("======文件名称:"+item+"======"); | 282 | LOGGER.info("======文件名称:"+item+"======"); |
| 283 | - List<Map<Integer, String>> listMap = EasyExcel.read(dirStr+item).headRowNumber(0).readHiddenRow(false).doReadSelectedSync(); | ||
| 284 | - for (Map<Integer, String> data : listMap) { | ||
| 285 | - // 返回每条数据的键值对 表示所在的列 和所在列的值 | ||
| 286 | - LOGGER.info(">{}", JSON.toJSONString(data)); | ||
| 287 | - } | 283 | + Map<Integer, List<Map<Integer, Object>>> listMap = EasyExcel.read(dirStr+item).headRowNumber(0).readHiddenRow(false).doReadAllSyncForMap(); |
| 284 | +// for (Map<Integer, String> data : listMap) { | ||
| 285 | +// // 返回每条数据的键值对 表示所在的列 和所在列的值 | ||
| 286 | +// //LOGGER.info(">{}", JSON.toJSONString(data)); | ||
| 287 | +// } | ||
| 288 | + LOGGER.info(">SUCCESS{}", JSON.toJSONString(listMap)); | ||
| 288 | }catch (Exception e) { | 289 | }catch (Exception e) { |
| 289 | e.printStackTrace(); | 290 | e.printStackTrace(); |
| 290 | } | 291 | } |