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 | 33 | import org.apache.poi.hssf.record.SSTRecord; |
| 34 | 34 | import org.apache.poi.hssf.record.StringRecord; |
| 35 | 35 | import org.apache.poi.hssf.record.TextObjectRecord; |
| 36 | +import org.apache.poi.hssf.record.WindowOneRecord; | |
| 36 | 37 | import org.slf4j.Logger; |
| 37 | 38 | import org.slf4j.LoggerFactory; |
| 38 | 39 | |
| ... | ... | @@ -83,6 +84,8 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { |
| 83 | 84 | private XlsReadContext xlsReadContext; |
| 84 | 85 | private static final Map<Short, XlsRecordHandler> XLS_RECORD_HANDLER_MAP = new HashMap<Short, XlsRecordHandler>(32); |
| 85 | 86 | List<Integer> skipCellRowIndexList = new ArrayList<Integer>(); |
| 87 | + private Integer activeSheetIndex = null; | |
| 88 | + private int currSheetIndex = -1; | |
| 86 | 89 | |
| 87 | 90 | static { |
| 88 | 91 | XLS_RECORD_HANDLER_MAP.put(BlankRecord.sid, new BlankRecordHandler()); |
| ... | ... | @@ -145,9 +148,14 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { |
| 145 | 148 | |
| 146 | 149 | @Override |
| 147 | 150 | public void processRecord(Record record) { |
| 151 | + //flush global data | |
| 152 | + this.initGlobalXlsData(xlsReadContext, record); | |
| 153 | + | |
| 154 | + //check whether skip | |
| 148 | 155 | if(this.needSkip(xlsReadContext, record)) { |
| 149 | 156 | return; |
| 150 | - } | |
| 157 | + } | |
| 158 | + | |
| 151 | 159 | XlsRecordHandler handler = XLS_RECORD_HANDLER_MAP.get(record.getSid()); |
| 152 | 160 | if (handler == null) { |
| 153 | 161 | return; |
| ... | ... | @@ -164,20 +172,39 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { |
| 164 | 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 | 185 | public boolean needSkip(XlsReadContext xlsReadContext, Record record) { |
| 168 | 186 | if(record.getSid() == RowRecord.sid) { |
| 169 | 187 | RowRecord rowRec = (RowRecord) record; |
| 170 | - if(!xlsReadContext.xlsReadWorkbookHolder().getReadHiddenRow() | |
| 188 | + Boolean readHiddenRow = xlsReadContext.xlsReadWorkbookHolder().getReadHiddenRow(); | |
| 189 | + if(readHiddenRow != null | |
| 190 | + && !readHiddenRow | |
| 171 | 191 | && rowRec.getZeroHeight()) { |
| 172 | 192 | skipCellRowIndexList.add(rowRec.getRowNumber()); |
| 173 | 193 | return true; |
| 174 | 194 | } |
| 175 | 195 | }else if(record instanceof CellRecord) { |
| 176 | 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 | 202 | return true; |
| 179 | - } | |
| 203 | + }else if(skipCellRowIndexList.contains(cellRec.getRow())) { | |
| 204 | + return true; | |
| 205 | + } | |
| 180 | 206 | } |
| 207 | + | |
| 181 | 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 | 253 | // } |
| 254 | 254 | |
| 255 | 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 | 257 | for (Map<Integer, String> data : listMap) { |
| 258 | 258 | // 返回每条数据的键值对 表示所在的列 和所在列的值 |
| 259 | 259 | LOGGER.info("读取到数据:{}", JSON.toJSONString(data)); | ... | ... |