Commit 21fd371554b60a603b645e444ff002493472f8c4
1 parent
f0ea537f
Exists in
master
.gitignore
Showing
12 changed files
with
108 additions
and
36 deletions
Show diff stats
.gitignore
bin/.gitignore
src/main/java/com/taover/easyexcel/analysis/ExcelAnalyserImpl.java
| ... | ... | @@ -111,6 +111,12 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { |
| 111 | 111 | } |
| 112 | 112 | analysisContext.readWorkbookHolder().setParameterSheetDataList(readSheetList); |
| 113 | 113 | analysisContext.readWorkbookHolder().setReadAll(readAll); |
| 114 | + for(ReadSheet item: readSheetList) { | |
| 115 | + if(item.getSheetSelected()) { | |
| 116 | + analysisContext.readWorkbookHolder().setReadJustSelected(true); | |
| 117 | + break; | |
| 118 | + } | |
| 119 | + } | |
| 114 | 120 | try { |
| 115 | 121 | excelReadExecutor.execute(); |
| 116 | 122 | } catch (ExcelAnalysisStopException e) { | ... | ... |
src/main/java/com/taover/easyexcel/analysis/v07/XlsxSaxAnalyser.java
| ... | ... | @@ -87,7 +87,7 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor { |
| 87 | 87 | if (!ite.hasNext()) { |
| 88 | 88 | throw new ExcelAnalysisException("Can not find any sheet!"); |
| 89 | 89 | } |
| 90 | - while (ite.hasNext()) { | |
| 90 | + while (ite.hasNext()) { | |
| 91 | 91 | InputStream inputStream = ite.next(); |
| 92 | 92 | sheetList.add(new ReadSheet(index, ite.getSheetName())); |
| 93 | 93 | sheetMap.put(index, inputStream); | ... | ... |
src/main/java/com/taover/easyexcel/analysis/v07/handlers/RowTagHandler.java
| ... | ... | @@ -23,7 +23,8 @@ public class RowTagHandler extends AbstractXlsxTagHandler { |
| 23 | 23 | |
| 24 | 24 | @Override |
| 25 | 25 | public boolean checkAndDoStartElementSkip(XlsxReadContext xlsxReadContext, String name, Attributes attributes) { |
| 26 | - if(!xlsxReadContext.xlsxReadWorkbookHolder().getReadHiddenRow() | |
| 26 | + if(xlsxReadContext.xlsxReadWorkbookHolder().getReadHiddenRow() != null | |
| 27 | + && !xlsxReadContext.xlsxReadWorkbookHolder().getReadHiddenRow() | |
| 27 | 28 | && isHiddenRow(attributes)) { |
| 28 | 29 | this.skipElementName = name; |
| 29 | 30 | return true; | ... | ... |
src/main/java/com/taover/easyexcel/analysis/v07/handlers/sax/XlsxRowHandler.java
| ... | ... | @@ -23,6 +23,7 @@ import com.taover.easyexcel.context.xlsx.XlsxReadContext; |
| 23 | 23 | * @author jipengfei |
| 24 | 24 | */ |
| 25 | 25 | public class XlsxRowHandler extends DefaultHandler { |
| 26 | + private static final boolean XLSX_DEBUB_PRINT_INFO = true; | |
| 26 | 27 | private XlsxReadContext xlsxReadContext; |
| 27 | 28 | private static final Map<String, XlsxTagHandler> XLSX_CELL_HANDLER_MAP = new HashMap<String, XlsxTagHandler>(32); |
| 28 | 29 | |
| ... | ... | @@ -59,6 +60,9 @@ public class XlsxRowHandler extends DefaultHandler { |
| 59 | 60 | |
| 60 | 61 | @Override |
| 61 | 62 | public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { |
| 63 | + if(XLSX_DEBUB_PRINT_INFO) { | |
| 64 | + this.printElement("startElement", uri, localName, name, attributes); | |
| 65 | + } | |
| 62 | 66 | XlsxTagHandler handler = XLSX_CELL_HANDLER_MAP.get(name); |
| 63 | 67 | if (handler == null |
| 64 | 68 | || !handler.support(xlsxReadContext) |
| ... | ... | @@ -69,9 +73,25 @@ public class XlsxRowHandler extends DefaultHandler { |
| 69 | 73 | handler.startElement(xlsxReadContext, name, attributes); |
| 70 | 74 | } |
| 71 | 75 | |
| 72 | - @Override | |
| 76 | + private void printElement(String title, String uri, String localName, String name, Attributes attributes) { | |
| 77 | + System.out.println("======"+title+"======"); | |
| 78 | + System.out.println("> uri:"+uri); | |
| 79 | + System.out.println("> localName:"+localName); | |
| 80 | + System.out.println("> name:"+name); | |
| 81 | + if(attributes == null) { | |
| 82 | + return; | |
| 83 | + } | |
| 84 | + for(int i=0; i<attributes.getLength(); ++i) { | |
| 85 | + System.out.println(">> attributes["+i+"]->uri:"+attributes.getURI(i)+",qName:"+attributes.getQName(i)+",value:"+attributes.getValue(i)); | |
| 86 | + } | |
| 87 | + } | |
| 88 | + | |
| 89 | + @Override | |
| 73 | 90 | public void characters(char[] ch, int start, int length) throws SAXException { |
| 74 | - String currentTag = xlsxReadContext.xlsxReadSheetHolder().getTagDeque().peek(); | |
| 91 | + if(XLSX_DEBUB_PRINT_INFO) { | |
| 92 | + this.printCharacters(ch); | |
| 93 | + } | |
| 94 | + String currentTag = xlsxReadContext.xlsxReadSheetHolder().getTagDeque().peek(); | |
| 75 | 95 | if (currentTag == null) { |
| 76 | 96 | return; |
| 77 | 97 | } |
| ... | ... | @@ -84,8 +104,16 @@ public class XlsxRowHandler extends DefaultHandler { |
| 84 | 104 | handler.characters(xlsxReadContext, ch, start, length); |
| 85 | 105 | } |
| 86 | 106 | |
| 87 | - @Override | |
| 107 | + private void printCharacters(char[] ch) { | |
| 108 | + System.out.println(">>>>>>characters>>>>>>"); | |
| 109 | + System.out.println(new String(ch)); | |
| 110 | + } | |
| 111 | + | |
| 112 | + @Override | |
| 88 | 113 | public void endElement(String uri, String localName, String name) throws SAXException { |
| 114 | + if(XLSX_DEBUB_PRINT_INFO) { | |
| 115 | + this.printElement("endElement", uri, localName, name, null); | |
| 116 | + } | |
| 89 | 117 | XlsxTagHandler handler = XLSX_CELL_HANDLER_MAP.get(name); |
| 90 | 118 | if (handler == null |
| 91 | 119 | || !handler.support(xlsxReadContext) | ... | ... |
src/main/java/com/taover/easyexcel/read/builder/ExcelReaderBuilder.java
| ... | ... | @@ -241,18 +241,26 @@ public class ExcelReaderBuilder extends AbstractExcelReaderParameterBuilder<Exce |
| 241 | 241 | } |
| 242 | 242 | |
| 243 | 243 | public ExcelReaderSheetBuilder sheet() { |
| 244 | - return sheet(null, null); | |
| 244 | + return sheet(null, null, true); | |
| 245 | 245 | } |
| 246 | 246 | |
| 247 | 247 | public ExcelReaderSheetBuilder sheet(Integer sheetNo) { |
| 248 | - return sheet(sheetNo, null); | |
| 248 | + return sheet(sheetNo, null, null); | |
| 249 | 249 | } |
| 250 | 250 | |
| 251 | 251 | public ExcelReaderSheetBuilder sheet(String sheetName) { |
| 252 | - return sheet(null, sheetName); | |
| 252 | + return sheet(null, sheetName, null); | |
| 253 | + } | |
| 254 | + | |
| 255 | + public ExcelReaderSheetBuilder sheet(Boolean sheetSelected) { | |
| 256 | + return sheet(null, null, sheetSelected); | |
| 253 | 257 | } |
| 254 | 258 | |
| 255 | 259 | public ExcelReaderSheetBuilder sheet(Integer sheetNo, String sheetName) { |
| 260 | + return sheet(sheetNo, sheetName, null); | |
| 261 | + } | |
| 262 | + | |
| 263 | + public ExcelReaderSheetBuilder sheet(Integer sheetNo, String sheetName, Boolean sheetSelected) { | |
| 256 | 264 | ExcelReaderSheetBuilder excelReaderSheetBuilder = new ExcelReaderSheetBuilder(build()); |
| 257 | 265 | if (sheetNo != null) { |
| 258 | 266 | excelReaderSheetBuilder.sheetNo(sheetNo); |
| ... | ... | @@ -260,6 +268,9 @@ public class ExcelReaderBuilder extends AbstractExcelReaderParameterBuilder<Exce |
| 260 | 268 | if (sheetName != null) { |
| 261 | 269 | excelReaderSheetBuilder.sheetName(sheetName); |
| 262 | 270 | } |
| 271 | + if (sheetSelected != null) { | |
| 272 | + excelReaderSheetBuilder.sheetSelected(sheetSelected); | |
| 273 | + } | |
| 263 | 274 | return excelReaderSheetBuilder; |
| 264 | 275 | } |
| 265 | 276 | ... | ... |
src/main/java/com/taover/easyexcel/read/builder/ExcelReaderSheetBuilder.java
| ... | ... | @@ -52,6 +52,11 @@ public class ExcelReaderSheetBuilder extends AbstractExcelReaderParameterBuilder |
| 52 | 52 | readSheet.setSheetName(sheetName); |
| 53 | 53 | return this; |
| 54 | 54 | } |
| 55 | + | |
| 56 | + public ExcelReaderSheetBuilder sheetSelected(Boolean sheetSelected) { | |
| 57 | + readSheet.setSheetSelected(sheetSelected); | |
| 58 | + return this; | |
| 59 | + } | |
| 55 | 60 | |
| 56 | 61 | public ReadSheet build() { |
| 57 | 62 | return readSheet; | ... | ... |
src/main/java/com/taover/easyexcel/read/metadata/ReadSheet.java
| ... | ... | @@ -14,7 +14,11 @@ public class ReadSheet extends ReadBasicParameter { |
| 14 | 14 | * sheet name |
| 15 | 15 | */ |
| 16 | 16 | private String sheetName; |
| 17 | - | |
| 17 | + /** | |
| 18 | + * sheet is selected | |
| 19 | + */ | |
| 20 | + private Boolean sheetSelected; | |
| 21 | + | |
| 18 | 22 | public ReadSheet() {} |
| 19 | 23 | |
| 20 | 24 | public ReadSheet(Integer sheetNo) { |
| ... | ... | @@ -26,7 +30,13 @@ public class ReadSheet extends ReadBasicParameter { |
| 26 | 30 | this.sheetName = sheetName; |
| 27 | 31 | } |
| 28 | 32 | |
| 29 | - public Integer getSheetNo() { | |
| 33 | + public ReadSheet(Integer sheetNo, String sheetName, Boolean sheetSelected) { | |
| 34 | + this.sheetNo = sheetNo; | |
| 35 | + this.sheetName = sheetName; | |
| 36 | + this.sheetSelected = sheetSelected; | |
| 37 | + } | |
| 38 | + | |
| 39 | + public Integer getSheetNo() { | |
| 30 | 40 | return sheetNo; |
| 31 | 41 | } |
| 32 | 42 | |
| ... | ... | @@ -41,8 +51,16 @@ public class ReadSheet extends ReadBasicParameter { |
| 41 | 51 | public void setSheetName(String sheetName) { |
| 42 | 52 | this.sheetName = sheetName; |
| 43 | 53 | } |
| 54 | + | |
| 55 | + public Boolean getSheetSelected() { | |
| 56 | + return sheetSelected; | |
| 57 | + } | |
| 58 | + | |
| 59 | + public void setSheetSelected(Boolean sheetSelected) { | |
| 60 | + this.sheetSelected = sheetSelected; | |
| 61 | + } | |
| 44 | 62 | |
| 45 | - public void copyBasicParameter(ReadSheet other) { | |
| 63 | + public void copyBasicParameter(ReadSheet other) { | |
| 46 | 64 | if (other == null) { |
| 47 | 65 | return; |
| 48 | 66 | } |
| ... | ... | @@ -57,6 +75,6 @@ public class ReadSheet extends ReadBasicParameter { |
| 57 | 75 | |
| 58 | 76 | @Override |
| 59 | 77 | public String toString() { |
| 60 | - return "ReadSheet{" + "sheetNo=" + sheetNo + ", sheetName='" + sheetName + '\'' + "} " + super.toString(); | |
| 78 | + return "ReadSheet{" + "sheetNo=" + sheetNo + ", sheetName='" + sheetName + '\'' + "sheetSelected=" + sheetSelected + "} " + super.toString(); | |
| 61 | 79 | } |
| 62 | 80 | } | ... | ... |
src/main/java/com/taover/easyexcel/read/metadata/holder/ReadWorkbookHolder.java
| ... | ... | @@ -100,6 +100,10 @@ public class ReadWorkbookHolder extends AbstractReadHolder { |
| 100 | 100 | * Read all |
| 101 | 101 | */ |
| 102 | 102 | private Boolean readAll; |
| 103 | + /** | |
| 104 | + * just remain selected sheet | |
| 105 | + */ | |
| 106 | + private Boolean readJustSelected; | |
| 103 | 107 | |
| 104 | 108 | /** |
| 105 | 109 | * Read hidden row |
| ... | ... | @@ -329,7 +333,15 @@ public class ReadWorkbookHolder extends AbstractReadHolder { |
| 329 | 333 | this.readAll = readAll; |
| 330 | 334 | } |
| 331 | 335 | |
| 332 | - @Override | |
| 336 | + public Boolean getReadJustSelected() { | |
| 337 | + return readJustSelected; | |
| 338 | + } | |
| 339 | + | |
| 340 | + public void setReadJustSelected(Boolean readJustSelected) { | |
| 341 | + this.readJustSelected = readJustSelected; | |
| 342 | + } | |
| 343 | + | |
| 344 | + @Override | |
| 333 | 345 | public HolderEnum holderType() { |
| 334 | 346 | return HolderEnum.WORKBOOK; |
| 335 | 347 | } | ... | ... |
src/main/java/com/taover/easyexcel/util/SheetUtils.java
| ... | ... | @@ -27,7 +27,7 @@ public class SheetUtils { |
| 27 | 27 | */ |
| 28 | 28 | public static ReadSheet match(ReadSheet readSheet, AnalysisContext analysisContext) { |
| 29 | 29 | ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder(); |
| 30 | - if (readWorkbookHolder.getReadAll()) { | |
| 30 | + if (readWorkbookHolder.getReadAll() || readWorkbookHolder.getReadJustSelected()) { | |
| 31 | 31 | return readSheet; |
| 32 | 32 | } |
| 33 | 33 | for (ReadSheet parameterReadSheet : readWorkbookHolder.getParameterSheetDataList()) { |
| ... | ... | @@ -37,7 +37,7 @@ public class SheetUtils { |
| 37 | 37 | if (parameterReadSheet.getSheetNo() == null && parameterReadSheet.getSheetName() == null) { |
| 38 | 38 | if (LOGGER.isDebugEnabled()) { |
| 39 | 39 | LOGGER.debug("The first is read by default."); |
| 40 | - } | |
| 40 | + } | |
| 41 | 41 | parameterReadSheet.setSheetNo(0); |
| 42 | 42 | } |
| 43 | 43 | boolean match = (parameterReadSheet.getSheetNo() != null | ... | ... |
src/test/java/com/taover/easyexcel/test/demo/read/ReadTest.java
| ... | ... | @@ -253,17 +253,18 @@ public class ReadTest { |
| 253 | 253 | // } |
| 254 | 254 | |
| 255 | 255 | // 这里 也可以不指定class,返回一个list,然后读取第一个sheet 同步读取会自动finish |
| 256 | -// List<Map<Integer, String>> listMap = EasyExcel.read(fileName).sheet().doReadSync(); | |
| 257 | -// for (Map<Integer, String> data : listMap) { | |
| 258 | -// // 返回每条数据的键值对 表示所在的列 和所在列的值 | |
| 259 | -// LOGGER.info("读取到数据:{}", JSON.toJSONString(data)); | |
| 260 | -// } | |
| 261 | - | |
| 262 | - Map<Integer, List<Map<Integer, Object>>> mapListMap = EasyExcel.read(fileName).doReadAllSyncForMap(); | |
| 263 | - for (Entry<Integer, List<Map<Integer, Object>>> item: mapListMap.entrySet()) { | |
| 256 | + List<Map<Integer, String>> listMap = EasyExcel.read("D://demo.xlsx").sheet().doReadSync(); | |
| 257 | + for (Map<Integer, String> data : listMap) { | |
| 264 | 258 | // 返回每条数据的键值对 表示所在的列 和所在列的值 |
| 265 | - LOGGER.info("读取到数据:{}\n{}", JSON.toJSONString(item.getKey()), JSON.toJSONString(item.getValue())); | |
| 259 | + LOGGER.info("读取到数据:{}", JSON.toJSONString(data)); | |
| 266 | 260 | } |
| 261 | + | |
| 262 | +// Map<Integer, List<Map<Integer, Object>>> mapListMap = EasyExcel.read(fileName).doReadAllSyncForMap(); | |
| 263 | +// Map<Integer, List<Map<Integer, Object>>> mapListMap = EasyExcel.read("D://demo.xlsx").doReadAllSyncForMap(); | |
| 264 | +// for (Entry<Integer, List<Map<Integer, Object>>> item: mapListMap.entrySet()) { | |
| 265 | +// // 返回每条数据的键值对 表示所在的列 和所在列的值 | |
| 266 | +// LOGGER.info("读取到数据:{}\n{}", JSON.toJSONString(item.getKey()), JSON.toJSONString(item.getValue())); | |
| 267 | +// } | |
| 267 | 268 | } |
| 268 | 269 | |
| 269 | 270 | /** | ... | ... |