Commit b769b0dc0d57420d676c685914dd9dcfaabff84e
1 parent
4e984701
Exists in
master
1.fix a bug about hidden line 2.default read active sheet
Showing
2 changed files
with
32 additions
and
5 deletions
Show diff stats
src/main/java/com/taover/easyexcel/analysis/v03/XlsSaxAnalyser.java
| @@ -33,6 +33,7 @@ import org.apache.poi.hssf.record.RowRecord; | @@ -33,6 +33,7 @@ import org.apache.poi.hssf.record.RowRecord; | ||
| 33 | import org.apache.poi.hssf.record.SSTRecord; | 33 | import org.apache.poi.hssf.record.SSTRecord; |
| 34 | import org.apache.poi.hssf.record.StringRecord; | 34 | import org.apache.poi.hssf.record.StringRecord; |
| 35 | import org.apache.poi.hssf.record.TextObjectRecord; | 35 | import org.apache.poi.hssf.record.TextObjectRecord; |
| 36 | +import org.apache.poi.hssf.record.WindowOneRecord; | ||
| 36 | import org.slf4j.Logger; | 37 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; | 38 | import org.slf4j.LoggerFactory; |
| 38 | 39 | ||
| @@ -83,6 +84,8 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { | @@ -83,6 +84,8 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { | ||
| 83 | private XlsReadContext xlsReadContext; | 84 | private XlsReadContext xlsReadContext; |
| 84 | private static final Map<Short, XlsRecordHandler> XLS_RECORD_HANDLER_MAP = new HashMap<Short, XlsRecordHandler>(32); | 85 | private static final Map<Short, XlsRecordHandler> XLS_RECORD_HANDLER_MAP = new HashMap<Short, XlsRecordHandler>(32); |
| 85 | List<Integer> skipCellRowIndexList = new ArrayList<Integer>(); | 86 | List<Integer> skipCellRowIndexList = new ArrayList<Integer>(); |
| 87 | + private Integer activeSheetIndex = null; | ||
| 88 | + private int currSheetIndex = -1; | ||
| 86 | 89 | ||
| 87 | static { | 90 | static { |
| 88 | XLS_RECORD_HANDLER_MAP.put(BlankRecord.sid, new BlankRecordHandler()); | 91 | XLS_RECORD_HANDLER_MAP.put(BlankRecord.sid, new BlankRecordHandler()); |
| @@ -145,9 +148,14 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { | @@ -145,9 +148,14 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { | ||
| 145 | 148 | ||
| 146 | @Override | 149 | @Override |
| 147 | public void processRecord(Record record) { | 150 | public void processRecord(Record record) { |
| 151 | + //flush global data | ||
| 152 | + this.initGlobalXlsData(xlsReadContext, record); | ||
| 153 | + | ||
| 154 | + //check whether skip | ||
| 148 | if(this.needSkip(xlsReadContext, record)) { | 155 | if(this.needSkip(xlsReadContext, record)) { |
| 149 | return; | 156 | return; |
| 150 | - } | 157 | + } |
| 158 | + | ||
| 151 | XlsRecordHandler handler = XLS_RECORD_HANDLER_MAP.get(record.getSid()); | 159 | XlsRecordHandler handler = XLS_RECORD_HANDLER_MAP.get(record.getSid()); |
| 152 | if (handler == null) { | 160 | if (handler == null) { |
| 153 | return; | 161 | return; |
| @@ -164,20 +172,39 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { | @@ -164,20 +172,39 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { | ||
| 164 | handler.processRecord(xlsReadContext, record); | 172 | handler.processRecord(xlsReadContext, record); |
| 165 | } | 173 | } |
| 166 | 174 | ||
| 175 | + private void initGlobalXlsData(XlsReadContext xlsReadContext2, Record record) { | ||
| 176 | + if(record.getSid() == EOFRecord.sid) { | ||
| 177 | + this.skipCellRowIndexList.clear(); | ||
| 178 | + ++this.currSheetIndex; | ||
| 179 | + } else if(record.getSid() == WindowOneRecord.sid) { | ||
| 180 | + WindowOneRecord window = (WindowOneRecord)record; | ||
| 181 | + this.activeSheetIndex = window.getActiveSheetIndex(); | ||
| 182 | + } | ||
| 183 | + } | ||
| 184 | + | ||
| 167 | public boolean needSkip(XlsReadContext xlsReadContext, Record record) { | 185 | public boolean needSkip(XlsReadContext xlsReadContext, Record record) { |
| 168 | if(record.getSid() == RowRecord.sid) { | 186 | if(record.getSid() == RowRecord.sid) { |
| 169 | RowRecord rowRec = (RowRecord) record; | 187 | RowRecord rowRec = (RowRecord) record; |
| 170 | - if(!xlsReadContext.xlsReadWorkbookHolder().getReadHiddenRow() | 188 | + Boolean readHiddenRow = xlsReadContext.xlsReadWorkbookHolder().getReadHiddenRow(); |
| 189 | + if(readHiddenRow != null | ||
| 190 | + && !readHiddenRow | ||
| 171 | && rowRec.getZeroHeight()) { | 191 | && rowRec.getZeroHeight()) { |
| 172 | skipCellRowIndexList.add(rowRec.getRowNumber()); | 192 | skipCellRowIndexList.add(rowRec.getRowNumber()); |
| 173 | return true; | 193 | return true; |
| 174 | } | 194 | } |
| 175 | }else if(record instanceof CellRecord) { | 195 | }else if(record instanceof CellRecord) { |
| 176 | CellRecord cellRec = (CellRecord)record; | 196 | CellRecord cellRec = (CellRecord)record; |
| 177 | - if(skipCellRowIndexList.contains(cellRec.getRow())) { | 197 | + Boolean justReadActiveSheet = xlsReadContext.xlsReadWorkbookHolder().getReadJustSelected(); |
| 198 | + if(justReadActiveSheet != null | ||
| 199 | + && justReadActiveSheet | ||
| 200 | + && this.activeSheetIndex != null | ||
| 201 | + && this.currSheetIndex != this.activeSheetIndex) { | ||
| 178 | return true; | 202 | return true; |
| 179 | - } | 203 | + }else if(skipCellRowIndexList.contains(cellRec.getRow())) { |
| 204 | + return true; | ||
| 205 | + } | ||
| 180 | } | 206 | } |
| 207 | + | ||
| 181 | return false; | 208 | return false; |
| 182 | } | 209 | } |
| 183 | } | 210 | } |
src/test/java/com/taover/easyexcel/test/demo/read/ReadTest.java
| @@ -253,7 +253,7 @@ public class ReadTest { | @@ -253,7 +253,7 @@ public class ReadTest { | ||
| 253 | // } | 253 | // } |
| 254 | 254 | ||
| 255 | // 这里 也可以不指定class,返回一个list,然后读取第一个sheet 同步读取会自动finish | 255 | // 这里 也可以不指定class,返回一个list,然后读取第一个sheet 同步读取会自动finish |
| 256 | - List<Map<Integer, String>> listMap = EasyExcel.read("D://demo.xlsx").headRowNumber(0).sheet(true).doReadSync(); | 256 | + List<Map<Integer, String>> listMap = EasyExcel.read("D://demo.xls").headRowNumber(0).readHiddenRow(false).sheet(true).doReadSync(); |
| 257 | for (Map<Integer, String> data : listMap) { | 257 | for (Map<Integer, String> data : listMap) { |
| 258 | // 返回每条数据的键值对 表示所在的列 和所在列的值 | 258 | // 返回每条数据的键值对 表示所在的列 和所在列的值 |
| 259 | LOGGER.info("读取到数据:{}", JSON.toJSONString(data)); | 259 | LOGGER.info("读取到数据:{}", JSON.toJSONString(data)); |