Commit a661d13ed602ee94718c7f316c5b549352bc9367

Authored by unknown
1 parent 6e8fabfa
Exists in master

xls 默认不读取隐藏行

bin/.gitignore
  1 +/com/
1 2 /converter/
2 3 /dataformat/
3 4 /demo/
... ... @@ -8,4 +9,3 @@
8 9 /multiplesheets/
9 10 /simple/
10 11 /template/
11   -/com/
... ...
src/main/java/com/taover/easyexcel/analysis/v03/XlsSaxAnalyser.java
1 1 package com.taover.easyexcel.analysis.v03;
2 2  
3 3 import java.io.IOException;
  4 +import java.util.ArrayList;
4 5 import java.util.HashMap;
5 6 import java.util.List;
6 7 import java.util.Map;
... ... @@ -11,11 +12,11 @@ import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
11 12 import org.apache.poi.hssf.eventusermodel.HSSFListener;
12 13 import org.apache.poi.hssf.eventusermodel.HSSFRequest;
13 14 import org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener;
14   -import org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord;
15 15 import org.apache.poi.hssf.record.BOFRecord;
16 16 import org.apache.poi.hssf.record.BlankRecord;
17 17 import org.apache.poi.hssf.record.BoolErrRecord;
18 18 import org.apache.poi.hssf.record.BoundSheetRecord;
  19 +import org.apache.poi.hssf.record.CellRecord;
19 20 import org.apache.poi.hssf.record.EOFRecord;
20 21 import org.apache.poi.hssf.record.FormulaRecord;
21 22 import org.apache.poi.hssf.record.HyperlinkRecord;
... ... @@ -28,6 +29,7 @@ import org.apache.poi.hssf.record.NumberRecord;
28 29 import org.apache.poi.hssf.record.ObjRecord;
29 30 import org.apache.poi.hssf.record.RKRecord;
30 31 import org.apache.poi.hssf.record.Record;
  32 +import org.apache.poi.hssf.record.RowRecord;
31 33 import org.apache.poi.hssf.record.SSTRecord;
32 34 import org.apache.poi.hssf.record.StringRecord;
33 35 import org.apache.poi.hssf.record.TextObjectRecord;
... ... @@ -80,7 +82,8 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
80 82 private static final short DUMMY_RECORD_SID = -1;
81 83 private XlsReadContext xlsReadContext;
82 84 private static final Map<Short, XlsRecordHandler> XLS_RECORD_HANDLER_MAP = new HashMap<Short, XlsRecordHandler>(32);
83   -
  85 + List<Integer> skipCellRowIndexList = new ArrayList<Integer>();
  86 +
84 87 static {
85 88 XLS_RECORD_HANDLER_MAP.put(BlankRecord.sid, new BlankRecordHandler());
86 89 XLS_RECORD_HANDLER_MAP.put(BOFRecord.sid, new BofRecordHandler());
... ... @@ -142,7 +145,10 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
142 145  
143 146 @Override
144 147 public void processRecord(Record record) {
145   - XlsRecordHandler handler = XLS_RECORD_HANDLER_MAP.get(record.getSid());
  148 + if(this.needSkip(xlsReadContext, record)) {
  149 + return;
  150 + }
  151 + XlsRecordHandler handler = XLS_RECORD_HANDLER_MAP.get(record.getSid());
146 152 if (handler == null) {
147 153 return;
148 154 }
... ... @@ -151,11 +157,27 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
151 157 if (ignoreRecord) {
152 158 // No need to read the current sheet
153 159 return;
154   - }
  160 + }
155 161 if (!handler.support(xlsReadContext, record)) {
156 162 return;
157 163 }
158 164 handler.processRecord(xlsReadContext, record);
159 165 }
160 166  
  167 + public boolean needSkip(XlsReadContext xlsReadContext, Record record) {
  168 + if(record.getSid() == RowRecord.sid) {
  169 + RowRecord rowRec = (RowRecord) record;
  170 + if(!xlsReadContext.xlsReadWorkbookHolder().getReadHiddenRow()
  171 + && rowRec.getZeroHeight()) {
  172 + skipCellRowIndexList.add(rowRec.getRowNumber());
  173 + return true;
  174 + }
  175 + }else if(record instanceof CellRecord) {
  176 + CellRecord cellRec = (CellRecord)record;
  177 + if(skipCellRowIndexList.contains(cellRec.getRow())) {
  178 + return true;
  179 + }
  180 + }
  181 + return false;
  182 + }
161 183 }
... ...
src/main/java/com/taover/easyexcel/analysis/v03/handlers/AbstractXlsRecordHandler.java
... ... @@ -10,10 +10,9 @@ import com.taover.easyexcel.context.xls.XlsReadContext;
10 10 *
11 11 * @author Jiaju Zhuang
12 12 **/
13   -public abstract class AbstractXlsRecordHandler implements XlsRecordHandler {
14   -
  13 +public abstract class AbstractXlsRecordHandler implements XlsRecordHandler {
15 14 @Override
16 15 public boolean support(XlsReadContext xlsReadContext, Record record) {
17 16 return true;
18   - }
  17 + }
19 18 }
... ...
src/test/java/com/taover/easyexcel/test/demo/read/ReadTest.java
... ... @@ -244,7 +244,7 @@ public class ReadTest {
244 244 */
245 245 @Test
246 246 public void synchronousRead() {
247   - String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
  247 + String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xls";
248 248 // 这里 需要指定读用哪个class去读,然后读取第一个sheet 同步读取会自动finish
249 249 List<DemoData> list = EasyExcel.read(fileName).head(DemoData.class).sheet().doReadSync();
250 250 for (DemoData data : list) {
... ... @@ -252,11 +252,11 @@ public class ReadTest {
252 252 }
253 253  
254 254 // 这里 也可以不指定class,返回一个list,然后读取第一个sheet 同步读取会自动finish
255   - List<Map<Integer, String>> listMap = EasyExcel.read(fileName).sheet().doReadSync();
256   - for (Map<Integer, String> data : listMap) {
257   - // 返回每条数据的键值对 表示所在的列 和所在列的值
258   - LOGGER.info("读取到数据:{}", JSON.toJSONString(data));
259   - }
  255 +// List<Map<Integer, String>> listMap = EasyExcel.read(fileName).sheet().doReadSync();
  256 +// for (Map<Integer, String> data : listMap) {
  257 +// // 返回每条数据的键值对 表示所在的列 和所在列的值
  258 +// LOGGER.info("读取到数据:{}", JSON.toJSONString(data));
  259 +// }
260 260 }
261 261  
262 262 /**
... ...
src/test/resources/demo/demo.xls 0 → 100644
No preview for this file type