Commit 4e9847010b4269afb1265543d5a2031c8eeed8fd
1 parent
05ceff0f
Exists in
master
1.提交代码
Showing
10 changed files
with
122 additions
and
43 deletions
Show diff stats
src/main/java/com/taover/easyexcel/analysis/ExcelAnalyserImpl.java
| ... | ... | @@ -112,12 +112,17 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { |
| 112 | 112 | analysisContext.readWorkbookHolder().setParameterSheetDataList(readSheetList); |
| 113 | 113 | analysisContext.readWorkbookHolder().setReadAll(readAll); |
| 114 | 114 | analysisContext.readWorkbookHolder().setReadJustSelected(false); |
| 115 | - for(ReadSheet item: readSheetList) { | |
| 116 | - if(item.getSheetSelected()) { | |
| 117 | - analysisContext.readWorkbookHolder().setReadJustSelected(true); | |
| 118 | - break; | |
| 119 | - } | |
| 115 | + | |
| 116 | + if(readSheetList != null) { | |
| 117 | + for(ReadSheet item: readSheetList) { | |
| 118 | + if(item.getSheetSelected() != null && item.getSheetSelected()) { | |
| 119 | + analysisContext.readWorkbookHolder().setReadJustSelected(true); | |
| 120 | + analysisContext.readWorkbookHolder().setReadAll(true); | |
| 121 | + break; | |
| 122 | + } | |
| 123 | + } | |
| 120 | 124 | } |
| 125 | + | |
| 121 | 126 | try { |
| 122 | 127 | excelReadExecutor.execute(); |
| 123 | 128 | } catch (ExcelAnalysisStopException e) { | ... | ... |
src/main/java/com/taover/easyexcel/analysis/v07/XlsxSaxAnalyser.java
| ... | ... | @@ -23,8 +23,11 @@ import org.apache.poi.xssf.usermodel.XSSFRelation; |
| 23 | 23 | import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook; |
| 24 | 24 | import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr; |
| 25 | 25 | import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument; |
| 26 | +import org.slf4j.Logger; | |
| 27 | +import org.slf4j.LoggerFactory; | |
| 26 | 28 | import org.xml.sax.ContentHandler; |
| 27 | 29 | import org.xml.sax.InputSource; |
| 30 | +import org.xml.sax.SAXException; | |
| 28 | 31 | import org.xml.sax.XMLReader; |
| 29 | 32 | |
| 30 | 33 | import com.taover.easyexcel.analysis.ExcelReadExecutor; |
| ... | ... | @@ -34,6 +37,7 @@ import com.taover.easyexcel.cache.ReadCache; |
| 34 | 37 | import com.taover.easyexcel.context.xlsx.XlsxReadContext; |
| 35 | 38 | import com.taover.easyexcel.enums.CellExtraTypeEnum; |
| 36 | 39 | import com.taover.easyexcel.exception.ExcelAnalysisException; |
| 40 | +import com.taover.easyexcel.exception.SheetNotSelectedException; | |
| 37 | 41 | import com.taover.easyexcel.metadata.CellExtra; |
| 38 | 42 | import com.taover.easyexcel.read.metadata.ReadSheet; |
| 39 | 43 | import com.taover.easyexcel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder; |
| ... | ... | @@ -46,7 +50,8 @@ import com.taover.easyexcel.util.StringUtils; |
| 46 | 50 | * @author jipengfei |
| 47 | 51 | */ |
| 48 | 52 | public class XlsxSaxAnalyser implements ExcelReadExecutor { |
| 49 | - | |
| 53 | + private static final Logger LOGGER = LoggerFactory.getLogger(XlsxSaxAnalyser.class); | |
| 54 | + | |
| 50 | 55 | private XlsxReadContext xlsxReadContext; |
| 51 | 56 | private List<ReadSheet> sheetList; |
| 52 | 57 | private Map<Integer, InputStream> sheetMap; |
| ... | ... | @@ -177,6 +182,10 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor { |
| 177 | 182 | xmlReader.setContentHandler(handler); |
| 178 | 183 | xmlReader.parse(inputSource); |
| 179 | 184 | inputStream.close(); |
| 185 | + } catch (SAXException e) { | |
| 186 | + if(e.getException() instanceof SheetNotSelectedException) { | |
| 187 | + LOGGER.warn(e.getMessage()); | |
| 188 | + } | |
| 180 | 189 | } catch (ExcelAnalysisException e) { |
| 181 | 190 | throw e; |
| 182 | 191 | } catch (Exception e) { |
| ... | ... | @@ -197,16 +206,12 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor { |
| 197 | 206 | for (ReadSheet readSheet : sheetList) { |
| 198 | 207 | readSheet = SheetUtils.match(readSheet, xlsxReadContext); |
| 199 | 208 | if (readSheet != null) { |
| 200 | - try { | |
| 201 | - xlsxReadContext.currentSheet(readSheet); | |
| 202 | - parseXmlSource(sheetMap.get(readSheet.getSheetNo()), new XlsxRowHandler(xlsxReadContext)); | |
| 203 | - // Read comments | |
| 204 | - readComments(readSheet); | |
| 205 | - // The last sheet is read | |
| 206 | - xlsxReadContext.analysisEventProcessor().endSheet(xlsxReadContext); | |
| 207 | - }catch (Exception e) { | |
| 208 | - e.printStackTrace(); | |
| 209 | - } | |
| 209 | + xlsxReadContext.currentSheet(readSheet); | |
| 210 | + parseXmlSource(sheetMap.get(readSheet.getSheetNo()), new XlsxRowHandler(xlsxReadContext)); | |
| 211 | + // Read comments | |
| 212 | + readComments(readSheet); | |
| 213 | + // The last sheet is read | |
| 214 | + xlsxReadContext.analysisEventProcessor().endSheet(xlsxReadContext); | |
| 210 | 215 | } |
| 211 | 216 | } |
| 212 | 217 | } | ... | ... |
src/main/java/com/taover/easyexcel/analysis/v07/handlers/AbstractXlsxTagHandler.java
| 1 | 1 | package com.taover.easyexcel.analysis.v07.handlers; |
| 2 | 2 | |
| 3 | 3 | import org.xml.sax.Attributes; |
| 4 | +import org.xml.sax.SAXException; | |
| 4 | 5 | |
| 5 | 6 | import com.taover.easyexcel.context.xlsx.XlsxReadContext; |
| 6 | 7 | |
| ... | ... | @@ -31,7 +32,7 @@ public abstract class AbstractXlsxTagHandler implements XlsxTagHandler { |
| 31 | 32 | } |
| 32 | 33 | |
| 33 | 34 | @Override |
| 34 | - public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) { | |
| 35 | + public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) throws SAXException { | |
| 35 | 36 | |
| 36 | 37 | } |
| 37 | 38 | ... | ... |
src/main/java/com/taover/easyexcel/analysis/v07/handlers/SheetViewHandler.java
0 → 100644
| ... | ... | @@ -0,0 +1,29 @@ |
| 1 | +package com.taover.easyexcel.analysis.v07.handlers; | |
| 2 | + | |
| 3 | +import org.xml.sax.Attributes; | |
| 4 | +import org.xml.sax.SAXException; | |
| 5 | + | |
| 6 | +import com.taover.easyexcel.constant.ExcelXmlConstants; | |
| 7 | +import com.taover.easyexcel.context.xlsx.XlsxReadContext; | |
| 8 | +import com.taover.easyexcel.exception.SheetNotSelectedException; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * Cell Handler | |
| 12 | + * | |
| 13 | + * @author jipengfei | |
| 14 | + */ | |
| 15 | +public class SheetViewHandler extends AbstractXlsxTagHandler { | |
| 16 | + | |
| 17 | + @Override | |
| 18 | + public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) throws SAXException { | |
| 19 | + //whether just read selected sheet | |
| 20 | + Boolean readJustSelected = xlsxReadContext.readWorkbookHolder().getReadJustSelected(); | |
| 21 | + if(ExcelXmlConstants.SHEET_VIEW_TAG.equals(name) | |
| 22 | + && readJustSelected != null | |
| 23 | + && readJustSelected | |
| 24 | + && !"1".equals(attributes.getValue(ExcelXmlConstants.ATTRIBUTE_TAB_SELECTED))) { | |
| 25 | + throw new SAXException("not selected sheet, just read selected sheet", new SheetNotSelectedException()); | |
| 26 | + } | |
| 27 | + } | |
| 28 | + | |
| 29 | +} | ... | ... |
src/main/java/com/taover/easyexcel/analysis/v07/handlers/XlsxTagHandler.java
| 1 | 1 | package com.taover.easyexcel.analysis.v07.handlers; |
| 2 | 2 | |
| 3 | 3 | import org.xml.sax.Attributes; |
| 4 | +import org.xml.sax.SAXException; | |
| 4 | 5 | |
| 5 | 6 | import com.taover.easyexcel.context.xlsx.XlsxReadContext; |
| 6 | 7 | |
| ... | ... | @@ -28,8 +29,9 @@ public interface XlsxTagHandler { |
| 28 | 29 | * Tag name |
| 29 | 30 | * @param attributes |
| 30 | 31 | * Tag attributes |
| 32 | + * @throws SAXException | |
| 31 | 33 | */ |
| 32 | - void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes); | |
| 34 | + void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) throws SAXException; | |
| 33 | 35 | |
| 34 | 36 | /** |
| 35 | 37 | * End handle | ... | ... |
src/main/java/com/taover/easyexcel/analysis/v07/handlers/sax/XlsxRowHandler.java
| ... | ... | @@ -3,6 +3,8 @@ package com.taover.easyexcel.analysis.v07.handlers.sax; |
| 3 | 3 | import java.util.HashMap; |
| 4 | 4 | import java.util.Map; |
| 5 | 5 | |
| 6 | +import org.slf4j.Logger; | |
| 7 | +import org.slf4j.LoggerFactory; | |
| 6 | 8 | import org.xml.sax.Attributes; |
| 7 | 9 | import org.xml.sax.SAXException; |
| 8 | 10 | import org.xml.sax.helpers.DefaultHandler; |
| ... | ... | @@ -15,6 +17,7 @@ import com.taover.easyexcel.analysis.v07.handlers.CountTagHandler; |
| 15 | 17 | import com.taover.easyexcel.analysis.v07.handlers.HyperlinkTagHandler; |
| 16 | 18 | import com.taover.easyexcel.analysis.v07.handlers.MergeCellTagHandler; |
| 17 | 19 | import com.taover.easyexcel.analysis.v07.handlers.RowTagHandler; |
| 20 | +import com.taover.easyexcel.analysis.v07.handlers.SheetViewHandler; | |
| 18 | 21 | import com.taover.easyexcel.analysis.v07.handlers.XlsxTagHandler; |
| 19 | 22 | import com.taover.easyexcel.constant.ExcelXmlConstants; |
| 20 | 23 | import com.taover.easyexcel.context.xlsx.XlsxReadContext; |
| ... | ... | @@ -23,7 +26,8 @@ import com.taover.easyexcel.context.xlsx.XlsxReadContext; |
| 23 | 26 | * @author jipengfei |
| 24 | 27 | */ |
| 25 | 28 | public class XlsxRowHandler extends DefaultHandler { |
| 26 | - private static final boolean XLSX_DEBUB_PRINT_INFO = false; | |
| 29 | + private static final Logger LOGGER = LoggerFactory.getLogger(XlsxRowHandler.class); | |
| 30 | + private static final boolean XLSX_DEBUG_PRINT_INFO = false; | |
| 27 | 31 | private XlsxReadContext xlsxReadContext; |
| 28 | 32 | private static final Map<String, XlsxTagHandler> XLSX_CELL_HANDLER_MAP = new HashMap<String, XlsxTagHandler>(32); |
| 29 | 33 | |
| ... | ... | @@ -52,6 +56,9 @@ public class XlsxRowHandler extends DefaultHandler { |
| 52 | 56 | RowTagHandler rowTagHandler = new RowTagHandler(); |
| 53 | 57 | XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.ROW_TAG, rowTagHandler); |
| 54 | 58 | XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_ROW_TAG, rowTagHandler); |
| 59 | + SheetViewHandler sheetViewHandler = new SheetViewHandler(); | |
| 60 | + XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.SHEET_VIEW_TAG, sheetViewHandler); | |
| 61 | + XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.X_SHEET_VIEW_TAG, sheetViewHandler); | |
| 55 | 62 | } |
| 56 | 63 | |
| 57 | 64 | public XlsxRowHandler(XlsxReadContext xlsxReadContext) { |
| ... | ... | @@ -60,20 +67,10 @@ public class XlsxRowHandler extends DefaultHandler { |
| 60 | 67 | |
| 61 | 68 | @Override |
| 62 | 69 | public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { |
| 63 | - if(XLSX_DEBUB_PRINT_INFO) { | |
| 70 | + if(XLSX_DEBUG_PRINT_INFO) { | |
| 64 | 71 | this.printElement("startElement", uri, localName, name, attributes); |
| 65 | 72 | } |
| 66 | 73 | |
| 67 | - //whether just read selected sheet | |
| 68 | - Boolean readJustSelected = this.xlsxReadContext.readWorkbookHolder().getReadJustSelected(); | |
| 69 | - if(ExcelXmlConstants.SHEET_VIEW_TAG.equals(name) | |
| 70 | - && readJustSelected != null | |
| 71 | - && readJustSelected | |
| 72 | - && !"1".equals(attributes.getValue("tabSelected"))) { | |
| 73 | - //throw new SAXException("not selected sheet, just read selected sheet"); | |
| 74 | - //return; | |
| 75 | - } | |
| 76 | - | |
| 77 | 74 | XlsxTagHandler handler = XLSX_CELL_HANDLER_MAP.get(name); |
| 78 | 75 | if (handler == null |
| 79 | 76 | || !handler.support(xlsxReadContext) |
| ... | ... | @@ -85,22 +82,22 @@ public class XlsxRowHandler extends DefaultHandler { |
| 85 | 82 | } |
| 86 | 83 | |
| 87 | 84 | private void printElement(String title, String uri, String localName, String name, Attributes attributes) { |
| 88 | - System.out.println("======"+title+"======"); | |
| 89 | - System.out.println("> uri:"+uri); | |
| 90 | - System.out.println("> localName:"+localName); | |
| 91 | - System.out.println("> name:"+name); | |
| 85 | + LOGGER.debug("======"+title+"======"); | |
| 86 | + LOGGER.debug("> uri:"+uri); | |
| 87 | + LOGGER.debug("> localName:"+localName); | |
| 88 | + LOGGER.debug("> name:"+name); | |
| 92 | 89 | if(attributes == null) { |
| 93 | 90 | return; |
| 94 | 91 | } |
| 95 | 92 | for(int i=0; i<attributes.getLength(); ++i) { |
| 96 | - System.out.println(">> attributes["+i+"]->uri:"+attributes.getURI(i)+",qName:"+attributes.getQName(i)+",value:"+attributes.getValue(i)); | |
| 93 | + LOGGER.debug(">> attributes["+i+"]->uri:"+attributes.getURI(i)+",qName:"+attributes.getQName(i)+",value:"+attributes.getValue(i)); | |
| 97 | 94 | } |
| 98 | 95 | } |
| 99 | 96 | |
| 100 | 97 | @Override |
| 101 | 98 | public void characters(char[] ch, int start, int length) throws SAXException { |
| 102 | - if(XLSX_DEBUB_PRINT_INFO) { | |
| 103 | - this.printCharacters(ch); | |
| 99 | + if(XLSX_DEBUG_PRINT_INFO) { | |
| 100 | + this.printCharacters(ch, start, length); | |
| 104 | 101 | } |
| 105 | 102 | String currentTag = xlsxReadContext.xlsxReadSheetHolder().getTagDeque().peek(); |
| 106 | 103 | if (currentTag == null) { |
| ... | ... | @@ -115,14 +112,14 @@ public class XlsxRowHandler extends DefaultHandler { |
| 115 | 112 | handler.characters(xlsxReadContext, ch, start, length); |
| 116 | 113 | } |
| 117 | 114 | |
| 118 | - private void printCharacters(char[] ch) { | |
| 119 | - System.out.println(">>>>>>characters>>>>>>"); | |
| 120 | - System.out.println(new String(ch)); | |
| 115 | + private void printCharacters(char[] ch, int start, int length) { | |
| 116 | + LOGGER.debug(">>>>>>characters>>>>>>"); | |
| 117 | + LOGGER.debug(new String(ch, start, length)); | |
| 121 | 118 | } |
| 122 | 119 | |
| 123 | 120 | @Override |
| 124 | 121 | public void endElement(String uri, String localName, String name) throws SAXException { |
| 125 | - if(XLSX_DEBUB_PRINT_INFO) { | |
| 122 | + if(XLSX_DEBUG_PRINT_INFO) { | |
| 126 | 123 | this.printElement("endElement", uri, localName, name, null); |
| 127 | 124 | } |
| 128 | 125 | XlsxTagHandler handler = XLSX_CELL_HANDLER_MAP.get(name); | ... | ... |
src/main/java/com/taover/easyexcel/constant/ExcelXmlConstants.java
src/main/java/com/taover/easyexcel/exception/SheetNotSelectedException.java
0 → 100644
| ... | ... | @@ -0,0 +1,22 @@ |
| 1 | +package com.taover.easyexcel.exception; | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * | |
| 5 | + * @author Wang bin | |
| 6 | + */ | |
| 7 | +public class SheetNotSelectedException extends RuntimeException { | |
| 8 | + | |
| 9 | + public SheetNotSelectedException() {} | |
| 10 | + | |
| 11 | + public SheetNotSelectedException(String message) { | |
| 12 | + super(message); | |
| 13 | + } | |
| 14 | + | |
| 15 | + public SheetNotSelectedException(String message, Throwable cause) { | |
| 16 | + super(message, cause); | |
| 17 | + } | |
| 18 | + | |
| 19 | + public SheetNotSelectedException(Throwable cause) { | |
| 20 | + super(cause); | |
| 21 | + } | |
| 22 | +} | ... | ... |
src/main/java/com/taover/easyexcel/util/SheetUtils.java
| ... | ... | @@ -27,9 +27,25 @@ 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() || readWorkbookHolder.getReadJustSelected()) { | |
| 30 | + //read all and not read just selected | |
| 31 | + if (readWorkbookHolder.getReadAll() && !readWorkbookHolder.getReadJustSelected()) { | |
| 31 | 32 | return readSheet; |
| 32 | 33 | } |
| 34 | + | |
| 35 | + //read just selected | |
| 36 | + if(readWorkbookHolder.getReadJustSelected()) { | |
| 37 | + for (ReadSheet parameterReadSheet : readWorkbookHolder.getParameterSheetDataList()) { | |
| 38 | + if (parameterReadSheet == null) { | |
| 39 | + continue; | |
| 40 | + } | |
| 41 | + if(parameterReadSheet.getSheetSelected() != null && parameterReadSheet.getSheetSelected()) { | |
| 42 | + readSheet.copyBasicParameter(parameterReadSheet); | |
| 43 | + return readSheet; | |
| 44 | + } | |
| 45 | + } | |
| 46 | + } | |
| 47 | + | |
| 48 | + //read by sheet no | |
| 33 | 49 | for (ReadSheet parameterReadSheet : readWorkbookHolder.getParameterSheetDataList()) { |
| 34 | 50 | if (parameterReadSheet == null) { |
| 35 | 51 | continue; | ... | ... |
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").sheet(true).doReadSync(); | |
| 256 | + List<Map<Integer, String>> listMap = EasyExcel.read("D://demo.xlsx").headRowNumber(0).sheet(true).doReadSync(); | |
| 257 | 257 | for (Map<Integer, String> data : listMap) { |
| 258 | 258 | // 返回每条数据的键值对 表示所在的列 和所在列的值 |
| 259 | 259 | LOGGER.info("读取到数据:{}", JSON.toJSONString(data)); | ... | ... |