Commit c14fb4ccfd311384f938c2e3321df49b9527e239
1 parent
d360ed14
Exists in
master
xlsx remove hidden sheet
Showing
14 changed files
with
328 additions
and
71 deletions
Show diff stats
src/main/java/com/taover/easyexcel/ExcelReader.java
... | ... | @@ -159,7 +159,14 @@ public class ExcelReader { |
159 | 159 | * Parse all sheet content by default |
160 | 160 | */ |
161 | 161 | public void readAll() { |
162 | - excelAnalyser.analysis(null, Boolean.TRUE); | |
162 | + excelAnalyser.analysis(null, Boolean.TRUE, Boolean.FALSE); | |
163 | + } | |
164 | + | |
165 | + /** | |
166 | + * Parse select sheet content by default | |
167 | + */ | |
168 | + public void readSelected() { | |
169 | + excelAnalyser.analysis(null, Boolean.FALSE, Boolean.TRUE); | |
163 | 170 | } |
164 | 171 | |
165 | 172 | /** |
... | ... | @@ -179,7 +186,7 @@ public class ExcelReader { |
179 | 186 | * @return |
180 | 187 | */ |
181 | 188 | public ExcelReader read(List<ReadSheet> readSheetList) { |
182 | - excelAnalyser.analysis(readSheetList, Boolean.FALSE); | |
189 | + excelAnalyser.analysis(readSheetList, Boolean.FALSE, Boolean.FALSE); | |
183 | 190 | return this; |
184 | 191 | } |
185 | 192 | ... | ... |
src/main/java/com/taover/easyexcel/analysis/ExcelAnalyser.java
... | ... | @@ -19,7 +19,7 @@ public interface ExcelAnalyser { |
19 | 19 | * @param readAll |
20 | 20 | * The <code>readSheetList</code> parameter is ignored, and all sheets are read. |
21 | 21 | */ |
22 | - void analysis(List<ReadSheet> readSheetList, Boolean readAll); | |
22 | + void analysis(List<ReadSheet> readSheetList, Boolean readAll, Boolean readSelected); | |
23 | 23 | |
24 | 24 | /** |
25 | 25 | * Complete the entire read file.Release the cache and close stream | ... | ... |
src/main/java/com/taover/easyexcel/analysis/ExcelAnalyserImpl.java
... | ... | @@ -104,24 +104,14 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { |
104 | 104 | } |
105 | 105 | |
106 | 106 | @Override |
107 | - public void analysis(List<ReadSheet> readSheetList, Boolean readAll) { | |
107 | + public void analysis(List<ReadSheet> readSheetList, Boolean readAll, Boolean readSelected) { | |
108 | 108 | try { |
109 | - if (!readAll && CollectionUtils.isEmpty(readSheetList)) { | |
109 | + if (CollectionUtils.isEmpty(readSheetList) && !readAll && !readSelected) { | |
110 | 110 | throw new IllegalArgumentException("Specify at least one read sheet."); |
111 | 111 | } |
112 | 112 | analysisContext.readWorkbookHolder().setParameterSheetDataList(readSheetList); |
113 | 113 | analysisContext.readWorkbookHolder().setReadAll(readAll); |
114 | - analysisContext.readWorkbookHolder().setReadJustSelected(false); | |
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 | - } | |
124 | - } | |
114 | + analysisContext.readWorkbookHolder().setReadJustSelected(readSelected); | |
125 | 115 | |
126 | 116 | try { |
127 | 117 | excelReadExecutor.execute(); | ... | ... |
src/main/java/com/taover/easyexcel/analysis/v03/XlsSaxAnalyser.java
... | ... | @@ -148,6 +148,8 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { |
148 | 148 | |
149 | 149 | @Override |
150 | 150 | public void processRecord(Record record) { |
151 | + System.out.println(record); | |
152 | + | |
151 | 153 | //flush global data |
152 | 154 | this.initGlobalXlsData(xlsReadContext, record); |
153 | 155 | |
... | ... | @@ -182,7 +184,7 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { |
182 | 184 | } |
183 | 185 | } |
184 | 186 | |
185 | - public boolean needSkip(XlsReadContext xlsReadContext, Record record) { | |
187 | + private boolean needSkip(XlsReadContext xlsReadContext, Record record) { | |
186 | 188 | if(record.getSid() == RowRecord.sid) { |
187 | 189 | RowRecord rowRec = (RowRecord) record; |
188 | 190 | Boolean readHiddenRow = xlsReadContext.xlsReadWorkbookHolder().getReadHiddenRow(); | ... | ... |
src/main/java/com/taover/easyexcel/analysis/v07/XlsxSaxAnalyser.java
... | ... | @@ -33,6 +33,8 @@ import org.xml.sax.XMLReader; |
33 | 33 | import com.taover.easyexcel.analysis.ExcelReadExecutor; |
34 | 34 | import com.taover.easyexcel.analysis.v07.handlers.sax.SharedStringsTableHandler; |
35 | 35 | import com.taover.easyexcel.analysis.v07.handlers.sax.XlsxRowHandler; |
36 | +import com.taover.easyexcel.analysis.v07.workbook.WorkbookAnalyser; | |
37 | +import com.taover.easyexcel.analysis.v07.workbook.WorkbookAnalyserImpl; | |
36 | 38 | import com.taover.easyexcel.cache.ReadCache; |
37 | 39 | import com.taover.easyexcel.context.xlsx.XlsxReadContext; |
38 | 40 | import com.taover.easyexcel.enums.CellExtraTypeEnum; |
... | ... | @@ -40,10 +42,10 @@ import com.taover.easyexcel.exception.ExcelAnalysisException; |
40 | 42 | import com.taover.easyexcel.exception.SheetNotSelectedException; |
41 | 43 | import com.taover.easyexcel.metadata.CellExtra; |
42 | 44 | import com.taover.easyexcel.read.metadata.ReadSheet; |
45 | +import com.taover.easyexcel.read.metadata.holder.ReadWorkbookHolder; | |
43 | 46 | import com.taover.easyexcel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder; |
44 | 47 | import com.taover.easyexcel.util.CollectionUtils; |
45 | 48 | import com.taover.easyexcel.util.FileUtils; |
46 | -import com.taover.easyexcel.util.SheetUtils; | |
47 | 49 | import com.taover.easyexcel.util.StringUtils; |
48 | 50 | |
49 | 51 | /** |
... | ... | @@ -92,9 +94,13 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor { |
92 | 94 | if (!ite.hasNext()) { |
93 | 95 | throw new ExcelAnalysisException("Can not find any sheet!"); |
94 | 96 | } |
95 | - while (ite.hasNext()) { | |
97 | + | |
98 | + WorkbookAnalyser wbAnalyser = new WorkbookAnalyserImpl(xssfReader.getWorkbookData()); | |
99 | + while (ite.hasNext()) { | |
96 | 100 | InputStream inputStream = ite.next(); |
97 | - sheetList.add(new ReadSheet(index, ite.getSheetName())); | |
101 | + boolean isHidden = wbAnalyser.isHiddenSheet(index); | |
102 | + boolean isActive = (index == wbAnalyser.getActiveTabIndexInSheetList()); | |
103 | + sheetList.add(new ReadSheet(index, ite.getSheetName(), isActive, isHidden)); | |
98 | 104 | sheetMap.put(index, inputStream); |
99 | 105 | if (xlsxReadContext.readWorkbookHolder().getExtraReadSet().contains(CellExtraTypeEnum.COMMENT)) { |
100 | 106 | CommentsTable commentsTable = ite.getSheetComments(); |
... | ... | @@ -203,20 +209,61 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor { |
203 | 209 | |
204 | 210 | @Override |
205 | 211 | public void execute() { |
206 | - for (ReadSheet readSheet : sheetList) { | |
207 | - readSheet = SheetUtils.match(readSheet, xlsxReadContext); | |
208 | - if (readSheet != null) { | |
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); | |
215 | - } | |
212 | + List<ReadSheet> readSheetList = this.filterSheetList(sheetList, xlsxReadContext); | |
213 | + for (ReadSheet readSheet : readSheetList) { | |
214 | + xlsxReadContext.currentSheet(readSheet); | |
215 | + parseXmlSource(sheetMap.get(readSheet.getSheetNo()), new XlsxRowHandler(xlsxReadContext)); | |
216 | + // Read comments | |
217 | + readComments(readSheet); | |
218 | + // The last sheet is read | |
219 | + xlsxReadContext.analysisEventProcessor().endSheet(xlsxReadContext); | |
216 | 220 | } |
217 | 221 | } |
218 | 222 | |
219 | - private void readComments(ReadSheet readSheet) { | |
223 | + private List<ReadSheet> filterSheetList(List<ReadSheet> sheetList, XlsxReadContext analysisContext) { | |
224 | + ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder(); | |
225 | + List<ReadSheet> sheetData = this.sheetList; | |
226 | + List<ReadSheet> result = new ArrayList<ReadSheet>(); | |
227 | + if(sheetData.isEmpty()) { | |
228 | + return result; | |
229 | + } | |
230 | + for(ReadSheet item: sheetData) { | |
231 | + if(readWorkbookHolder.getReadAll()) { | |
232 | + if(!item.getSheetHidden()) { | |
233 | + result.add(item); | |
234 | + } | |
235 | + }else if(readWorkbookHolder.getReadJustSelected()) { | |
236 | + if(item.getSheetSelected()) { | |
237 | + result.add(item); | |
238 | + } | |
239 | + }else { | |
240 | + for(ReadSheet innerItem: sheetList) { | |
241 | + boolean match = (item.getSheetNo() != null && item.getSheetNo().equals(innerItem.getSheetNo())); | |
242 | + if (!match) { | |
243 | + String parameterSheetName = item.getSheetName(); | |
244 | + if (!StringUtils.isEmpty(parameterSheetName)) { | |
245 | + boolean autoTrim = (item.getAutoTrim() != null && item.getAutoTrim()) | |
246 | + || (item.getAutoTrim() == null && analysisContext.readWorkbookHolder().getGlobalConfiguration().getAutoTrim()); | |
247 | + if (autoTrim) { | |
248 | + parameterSheetName = parameterSheetName.trim(); | |
249 | + } | |
250 | + match = parameterSheetName.equals(innerItem.getSheetName()); | |
251 | + } | |
252 | + } | |
253 | + if (match) { | |
254 | + item.copyBasicParameter(innerItem); | |
255 | + result.add(innerItem); | |
256 | + } | |
257 | + } | |
258 | + } | |
259 | + } | |
260 | + if(result.isEmpty()) { | |
261 | + result.add(sheetData.get(0)); | |
262 | + } | |
263 | + return result; | |
264 | + } | |
265 | + | |
266 | + private void readComments(ReadSheet readSheet) { | |
220 | 267 | if (!xlsxReadContext.readWorkbookHolder().getExtraReadSet().contains(CellExtraTypeEnum.COMMENT)) { |
221 | 268 | return; |
222 | 269 | } | ... | ... |
src/main/java/com/taover/easyexcel/analysis/v07/handlers/sax/XlsxRowHandler.java
... | ... | @@ -3,8 +3,6 @@ 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; | |
8 | 6 | import org.xml.sax.Attributes; |
9 | 7 | import org.xml.sax.SAXException; |
10 | 8 | import org.xml.sax.helpers.DefaultHandler; |
... | ... | @@ -26,7 +24,6 @@ import com.taover.easyexcel.context.xlsx.XlsxReadContext; |
26 | 24 | * @author jipengfei |
27 | 25 | */ |
28 | 26 | public class XlsxRowHandler extends DefaultHandler { |
29 | - private static final Logger LOGGER = LoggerFactory.getLogger(XlsxRowHandler.class); | |
30 | 27 | private static final boolean XLSX_DEBUG_PRINT_INFO = false; |
31 | 28 | private XlsxReadContext xlsxReadContext; |
32 | 29 | private static final Map<String, XlsxTagHandler> XLSX_CELL_HANDLER_MAP = new HashMap<String, XlsxTagHandler>(32); |
... | ... | @@ -82,15 +79,15 @@ public class XlsxRowHandler extends DefaultHandler { |
82 | 79 | } |
83 | 80 | |
84 | 81 | private void printElement(String title, String uri, String localName, String name, Attributes attributes) { |
85 | - LOGGER.debug("======"+title+"======"); | |
86 | - LOGGER.debug("> uri:"+uri); | |
87 | - LOGGER.debug("> localName:"+localName); | |
88 | - LOGGER.debug("> name:"+name); | |
82 | + System.out.println("======"+title+"======"); | |
83 | + System.out.println("> uri:"+uri); | |
84 | + System.out.println("> localName:"+localName); | |
85 | + System.out.println("> name:"+name); | |
89 | 86 | if(attributes == null) { |
90 | 87 | return; |
91 | 88 | } |
92 | 89 | for(int i=0; i<attributes.getLength(); ++i) { |
93 | - LOGGER.debug(">> attributes["+i+"]->uri:"+attributes.getURI(i)+",qName:"+attributes.getQName(i)+",value:"+attributes.getValue(i)); | |
90 | + System.out.println(">> attributes["+i+"]->uri:"+attributes.getURI(i)+",localname:"+attributes.getLocalName(i)+",qName:"+attributes.getQName(i)+",type:"+attributes.getType(i)+",value:"+attributes.getValue(i)); | |
94 | 91 | } |
95 | 92 | } |
96 | 93 | |
... | ... | @@ -113,8 +110,8 @@ public class XlsxRowHandler extends DefaultHandler { |
113 | 110 | } |
114 | 111 | |
115 | 112 | private void printCharacters(char[] ch, int start, int length) { |
116 | - LOGGER.debug(">>>>>>characters>>>>>>"); | |
117 | - LOGGER.debug(new String(ch, start, length)); | |
113 | + System.out.println(">>>>>>characters>>>>>>"); | |
114 | + System.out.println(new String(ch, start, length)); | |
118 | 115 | } |
119 | 116 | |
120 | 117 | @Override | ... | ... |
src/main/java/com/taover/easyexcel/analysis/v07/metadata/WorkbookSheet.java
0 → 100644
... | ... | @@ -0,0 +1,58 @@ |
1 | +package com.taover.easyexcel.analysis.v07.metadata; | |
2 | + | |
3 | +import org.w3c.dom.NamedNodeMap; | |
4 | +import org.w3c.dom.Node; | |
5 | + | |
6 | +public class WorkbookSheet { | |
7 | + private String name; | |
8 | + private String sheetId; | |
9 | + private String state; | |
10 | + | |
11 | + public WorkbookSheet(String name, String sheetId, String state) { | |
12 | + super(); | |
13 | + this.name = name; | |
14 | + this.sheetId = sheetId; | |
15 | + this.state = state; | |
16 | + } | |
17 | + | |
18 | + public WorkbookSheet(Node node) { | |
19 | + NamedNodeMap attrs = node.getAttributes(); | |
20 | + Node nameNode = attrs.getNamedItem("name"); | |
21 | + Node sheetIdNode = attrs.getNamedItem("sheetId"); | |
22 | + Node stateNode = attrs.getNamedItem("state"); | |
23 | + if(nameNode != null) { | |
24 | + this.name = nameNode.getNodeValue(); | |
25 | + }else { | |
26 | + this.name = ""; | |
27 | + } | |
28 | + if(sheetIdNode != null) { | |
29 | + this.sheetId = sheetIdNode.getNodeValue(); | |
30 | + }else { | |
31 | + this.sheetId = ""; | |
32 | + } | |
33 | + if(stateNode != null) { | |
34 | + this.state = stateNode.getNodeValue(); | |
35 | + }else { | |
36 | + this.state = ""; | |
37 | + } | |
38 | + } | |
39 | + | |
40 | + public String getState() { | |
41 | + return state; | |
42 | + } | |
43 | + public void setState(String state) { | |
44 | + this.state = state; | |
45 | + } | |
46 | + public String getName() { | |
47 | + return name; | |
48 | + } | |
49 | + public void setName(String name) { | |
50 | + this.name = name; | |
51 | + } | |
52 | + public String getSheetId() { | |
53 | + return sheetId; | |
54 | + } | |
55 | + public void setSheetId(String sheetId) { | |
56 | + this.sheetId = sheetId; | |
57 | + } | |
58 | +} | ... | ... |
src/main/java/com/taover/easyexcel/analysis/v07/workbook/WorkbookAnalyser.java
0 → 100644
... | ... | @@ -0,0 +1,13 @@ |
1 | +package com.taover.easyexcel.analysis.v07.workbook; | |
2 | + | |
3 | +import java.util.List; | |
4 | + | |
5 | +import com.taover.easyexcel.analysis.v07.metadata.WorkbookSheet; | |
6 | + | |
7 | +public interface WorkbookAnalyser { | |
8 | + int getActiveTabIndexInSheetList(); | |
9 | + | |
10 | + List<WorkbookSheet> getSheetList(); | |
11 | + | |
12 | + boolean isHiddenSheet(int sheetIndex); | |
13 | +} | ... | ... |
src/main/java/com/taover/easyexcel/analysis/v07/workbook/WorkbookAnalyserImpl.java
0 → 100644
... | ... | @@ -0,0 +1,123 @@ |
1 | +package com.taover.easyexcel.analysis.v07.workbook; | |
2 | + | |
3 | +import java.io.IOException; | |
4 | +import java.io.InputStream; | |
5 | +import java.util.ArrayList; | |
6 | +import java.util.List; | |
7 | + | |
8 | +import javax.xml.parsers.DocumentBuilderFactory; | |
9 | +import javax.xml.parsers.ParserConfigurationException; | |
10 | + | |
11 | +import org.w3c.dom.Document; | |
12 | +import org.w3c.dom.NamedNodeMap; | |
13 | +import org.w3c.dom.Node; | |
14 | +import org.w3c.dom.NodeList; | |
15 | +import org.xml.sax.SAXException; | |
16 | + | |
17 | +import com.taover.easyexcel.analysis.v07.metadata.WorkbookSheet; | |
18 | + | |
19 | +public class WorkbookAnalyserImpl implements WorkbookAnalyser { | |
20 | + private static String TAG_NAME_WORKBOOK = "workbook"; | |
21 | + private static String TAG_NAME_WORKBOOKVIEW = "workbookView"; | |
22 | + private static String TAG_NAME_ACTIVETAB = "activeTab"; | |
23 | + private static String TAG_NAME_SHEETS = "sheets"; | |
24 | + private static String TAG_NAME_BOOKVIEWS = "bookViews"; | |
25 | + private static String ATTR_STATE_V_HIDDEN = "hidden"; | |
26 | + | |
27 | + private int activeTabIndex; | |
28 | + private List<WorkbookSheet> sheetList; | |
29 | + | |
30 | + public WorkbookAnalyserImpl(InputStream workbookInputStream) throws SAXException, IOException, ParserConfigurationException { | |
31 | + Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(workbookInputStream); | |
32 | + NodeList nList = doc.getElementsByTagName(TAG_NAME_WORKBOOK); | |
33 | + Node workbookNode = nList.item(0); | |
34 | + this.initSheetList(this.getSheetsDoc(workbookNode.getOwnerDocument())); | |
35 | + this.initActiveTabIndex(this.getWorkbookViewDoc(workbookNode.getOwnerDocument())); | |
36 | + } | |
37 | + | |
38 | + private void initSheetList(List<Node> sheetsDoc) { | |
39 | + this.sheetList = new ArrayList<WorkbookSheet>(); | |
40 | + if(sheetsDoc == null || sheetsDoc.isEmpty()) { | |
41 | + return; | |
42 | + } | |
43 | + for(int i=0; i<sheetsDoc.size(); ++i) { | |
44 | + this.sheetList.add(new WorkbookSheet(sheetsDoc.get(i))); | |
45 | + } | |
46 | + } | |
47 | + | |
48 | + private void initActiveTabIndex(List<Node> workbookViewDoc) { | |
49 | + if(workbookViewDoc == null || workbookViewDoc.isEmpty()) { | |
50 | + this.activeTabIndex = 0; | |
51 | + } | |
52 | + Integer activeTab = null; | |
53 | + for(int i=0; i<workbookViewDoc.size(); ++i) { | |
54 | + Node item = workbookViewDoc.get(i); | |
55 | + if(item.getNodeName().equals(TAG_NAME_WORKBOOKVIEW)) { | |
56 | + NamedNodeMap attrs = item.getAttributes(); | |
57 | + if(attrs == null) { | |
58 | + continue; | |
59 | + } | |
60 | + Node activeTabNode = attrs.getNamedItem(TAG_NAME_ACTIVETAB); | |
61 | + if(activeTabNode != null) { | |
62 | + try { | |
63 | + activeTab = Integer.valueOf(activeTabNode.getNodeValue()); | |
64 | + }catch (Exception e) { | |
65 | + continue; | |
66 | + } | |
67 | + } | |
68 | + } | |
69 | + } | |
70 | + if(activeTab == null) { | |
71 | + this.activeTabIndex = 0; | |
72 | + }else { | |
73 | + int hiddenSheetNum = 0; | |
74 | + for(int i=0; i<this.sheetList.size() && i<activeTab.intValue(); ++i) { | |
75 | + if(this.sheetList.get(i).getState().equals(ATTR_STATE_V_HIDDEN)) { | |
76 | + ++hiddenSheetNum; | |
77 | + } | |
78 | + } | |
79 | + this.activeTabIndex = hiddenSheetNum + activeTab - 1; | |
80 | + } | |
81 | + } | |
82 | + | |
83 | + private List<Node> getSheetsDoc(Document workbook) { | |
84 | + NodeList sheetsNodeList = workbook.getElementsByTagName(TAG_NAME_SHEETS); | |
85 | + Node sheetsNode = sheetsNodeList.item(0); | |
86 | + NodeList sheetNodeList = sheetsNode.getChildNodes(); | |
87 | + List<Node> result = new ArrayList<Node>(); | |
88 | + for(int i=0; i<sheetNodeList.getLength(); ++i) { | |
89 | + result.add(sheetNodeList.item(i)); | |
90 | + } | |
91 | + return result; | |
92 | + } | |
93 | + | |
94 | + private List<Node> getWorkbookViewDoc(Document workbook) { | |
95 | + NodeList sheetsNodeList = workbook.getElementsByTagName(TAG_NAME_BOOKVIEWS); | |
96 | + Node sheetsNode = sheetsNodeList.item(0); | |
97 | + NodeList sheetNodeList = sheetsNode.getChildNodes(); | |
98 | + List<Node> result = new ArrayList<Node>(); | |
99 | + for(int i=0; i<sheetNodeList.getLength(); ++i) { | |
100 | + result.add(sheetNodeList.item(i)); | |
101 | + } | |
102 | + return result; | |
103 | + } | |
104 | + | |
105 | + @Override | |
106 | + public int getActiveTabIndexInSheetList() { | |
107 | + return this.activeTabIndex; | |
108 | + } | |
109 | + | |
110 | + @Override | |
111 | + public List<WorkbookSheet> getSheetList() { | |
112 | + return sheetList; | |
113 | + } | |
114 | + | |
115 | + @Override | |
116 | + public boolean isHiddenSheet(int sheetIndex) { | |
117 | + if(sheetIndex < 0 || sheetIndex >= this.sheetList.size()) { | |
118 | + return false; | |
119 | + } | |
120 | + return this.sheetList.get(sheetIndex).getState().equals(ATTR_STATE_V_HIDDEN); | |
121 | + } | |
122 | + | |
123 | +} | ... | ... |
src/main/java/com/taover/easyexcel/read/builder/ExcelReaderBuilder.java
... | ... | @@ -231,6 +231,20 @@ public class ExcelReaderBuilder extends AbstractExcelReaderParameterBuilder<Exce |
231 | 231 | * |
232 | 232 | * @return |
233 | 233 | */ |
234 | + public <T> List<T> doReadSelectedSync() { | |
235 | + SyncReadListener syncReadListener = new SyncReadListener(); | |
236 | + registerReadListener(syncReadListener); | |
237 | + ExcelReader excelReader = build(); | |
238 | + excelReader.readSelected(); | |
239 | + excelReader.finish(); | |
240 | + return (List<T>)syncReadListener.getList(); | |
241 | + } | |
242 | + | |
243 | + /** | |
244 | + * Synchronous reads return results | |
245 | + * | |
246 | + * @return | |
247 | + */ | |
234 | 248 | public Map<Integer, List<Map<Integer, Object>>> doReadAllSyncForMap() { |
235 | 249 | SyncReadAllSheetToMapListener syncReadListener = new SyncReadAllSheetToMapListener(); |
236 | 250 | registerReadListener(syncReadListener); |
... | ... | @@ -241,26 +255,18 @@ public class ExcelReaderBuilder extends AbstractExcelReaderParameterBuilder<Exce |
241 | 255 | } |
242 | 256 | |
243 | 257 | public ExcelReaderSheetBuilder sheet() { |
244 | - return sheet(null, null, true); | |
258 | + return sheet(null, null); | |
245 | 259 | } |
246 | 260 | |
247 | 261 | public ExcelReaderSheetBuilder sheet(Integer sheetNo) { |
248 | - return sheet(sheetNo, null, null); | |
262 | + return sheet(sheetNo, null); | |
249 | 263 | } |
250 | 264 | |
251 | 265 | public ExcelReaderSheetBuilder sheet(String sheetName) { |
252 | - return sheet(null, sheetName, null); | |
266 | + return sheet(null, sheetName); | |
253 | 267 | } |
254 | 268 | |
255 | - public ExcelReaderSheetBuilder sheet(Boolean sheetSelected) { | |
256 | - return sheet(null, null, sheetSelected); | |
257 | - } | |
258 | - | |
259 | 269 | 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) { | |
264 | 270 | ExcelReaderSheetBuilder excelReaderSheetBuilder = new ExcelReaderSheetBuilder(build()); |
265 | 271 | if (sheetNo != null) { |
266 | 272 | excelReaderSheetBuilder.sheetNo(sheetNo); |
... | ... | @@ -268,9 +274,6 @@ public class ExcelReaderBuilder extends AbstractExcelReaderParameterBuilder<Exce |
268 | 274 | if (sheetName != null) { |
269 | 275 | excelReaderSheetBuilder.sheetName(sheetName); |
270 | 276 | } |
271 | - if (sheetSelected != null) { | |
272 | - excelReaderSheetBuilder.sheetSelected(sheetSelected); | |
273 | - } | |
274 | 277 | return excelReaderSheetBuilder; |
275 | 278 | } |
276 | 279 | ... | ... |
src/main/java/com/taover/easyexcel/read/builder/ExcelReaderSheetBuilder.java
1 | 1 | package com.taover.easyexcel.read.builder; |
2 | 2 | |
3 | 3 | import java.util.List; |
4 | -import java.util.Map; | |
5 | 4 | |
6 | 5 | import com.taover.easyexcel.ExcelReader; |
7 | -import com.taover.easyexcel.event.SyncReadAllSheetToMapListener; | |
8 | 6 | import com.taover.easyexcel.event.SyncReadListener; |
9 | 7 | import com.taover.easyexcel.exception.ExcelAnalysisException; |
10 | 8 | import com.taover.easyexcel.exception.ExcelGenerateException; |
... | ... | @@ -52,11 +50,6 @@ public class ExcelReaderSheetBuilder extends AbstractExcelReaderParameterBuilder |
52 | 50 | readSheet.setSheetName(sheetName); |
53 | 51 | return this; |
54 | 52 | } |
55 | - | |
56 | - public ExcelReaderSheetBuilder sheetSelected(Boolean sheetSelected) { | |
57 | - readSheet.setSheetSelected(sheetSelected); | |
58 | - return this; | |
59 | - } | |
60 | 53 | |
61 | 54 | public ReadSheet build() { |
62 | 55 | return readSheet; | ... | ... |
src/main/java/com/taover/easyexcel/read/metadata/ReadSheet.java
... | ... | @@ -18,22 +18,26 @@ public class ReadSheet extends ReadBasicParameter { |
18 | 18 | * sheet is selected |
19 | 19 | */ |
20 | 20 | private Boolean sheetSelected; |
21 | + /** | |
22 | + * sheet is hidden | |
23 | + */ | |
24 | + private Boolean sheetHidden; | |
21 | 25 | |
22 | 26 | public ReadSheet() {} |
23 | 27 | |
24 | 28 | public ReadSheet(Integer sheetNo) { |
25 | - this.sheetNo = sheetNo; | |
29 | + this(sheetNo, null, null, null); | |
26 | 30 | } |
27 | 31 | |
28 | 32 | public ReadSheet(Integer sheetNo, String sheetName) { |
29 | - this.sheetNo = sheetNo; | |
30 | - this.sheetName = sheetName; | |
33 | + this(sheetNo, sheetName, null, null); | |
31 | 34 | } |
32 | - | |
33 | - public ReadSheet(Integer sheetNo, String sheetName, Boolean sheetSelected) { | |
35 | + | |
36 | + public ReadSheet(Integer sheetNo, String sheetName, Boolean sheetSelected, Boolean sheetHidden) { | |
34 | 37 | this.sheetNo = sheetNo; |
35 | 38 | this.sheetName = sheetName; |
36 | 39 | this.sheetSelected = sheetSelected; |
40 | + this.sheetHidden = sheetHidden; | |
37 | 41 | } |
38 | 42 | |
39 | 43 | public Integer getSheetNo() { |
... | ... | @@ -59,6 +63,14 @@ public class ReadSheet extends ReadBasicParameter { |
59 | 63 | public void setSheetSelected(Boolean sheetSelected) { |
60 | 64 | this.sheetSelected = sheetSelected; |
61 | 65 | } |
66 | + | |
67 | + public Boolean getSheetHidden() { | |
68 | + return sheetHidden; | |
69 | + } | |
70 | + | |
71 | + public void setSheetHidden(Boolean sheetHidden) { | |
72 | + this.sheetHidden = sheetHidden; | |
73 | + } | |
62 | 74 | |
63 | 75 | public void copyBasicParameter(ReadSheet other) { |
64 | 76 | if (other == null) { | ... | ... |
src/main/java/com/taover/easyexcel/util/SheetUtils.java
... | ... | @@ -28,10 +28,14 @@ public class SheetUtils { |
28 | 28 | public static ReadSheet match(ReadSheet readSheet, AnalysisContext analysisContext) { |
29 | 29 | ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder(); |
30 | 30 | //read all and not read just selected |
31 | - if (readWorkbookHolder.getReadAll() && !readWorkbookHolder.getReadJustSelected()) { | |
31 | + if (readWorkbookHolder.getReadAll()) { | |
32 | 32 | return readSheet; |
33 | 33 | } |
34 | 34 | |
35 | + if(readWorkbookHolder.getParameterSheetDataList() == null) { | |
36 | + return null; | |
37 | + } | |
38 | + | |
35 | 39 | //read just selected |
36 | 40 | if(readWorkbookHolder.getReadJustSelected()) { |
37 | 41 | for (ReadSheet parameterReadSheet : readWorkbookHolder.getParameterSheetDataList()) { |
... | ... | @@ -47,6 +51,7 @@ public class SheetUtils { |
47 | 51 | |
48 | 52 | //read by sheet no |
49 | 53 | for (ReadSheet parameterReadSheet : readWorkbookHolder.getParameterSheetDataList()) { |
54 | + //remove null or hidden sheet | |
50 | 55 | if (parameterReadSheet == null) { |
51 | 56 | continue; |
52 | 57 | } | ... | ... |
src/test/java/com/taover/easyexcel/test/demo/read/ReadTest.java
... | ... | @@ -252,12 +252,19 @@ public class ReadTest { |
252 | 252 | // } |
253 | 253 | |
254 | 254 | // 这里 也可以不指定class,返回一个list,然后读取第一个sheet 同步读取会自动finish |
255 | - List<Map<Integer, String>> listMap = EasyExcel.read("D://debug//suffixwrong//demo.xls").headRowNumber(0).readHiddenRow(false).sheet(true).doReadSync(); | |
255 | + List<Map<Integer, String>> listMap = EasyExcel.read("D://debug//hiddensheet//demo.xls").headRowNumber(0).readHiddenRow(false).doReadSelectedSync(); | |
256 | 256 | for (Map<Integer, String> data : listMap) { |
257 | 257 | // 返回每条数据的键值对 表示所在的列 和所在列的值 |
258 | 258 | LOGGER.info("读取到数据:{}", JSON.toJSONString(data)); |
259 | 259 | } |
260 | 260 | |
261 | +// Map<Integer, List<Map<Integer, Object>>> map = EasyExcel.read("D://debug//hiddensheet//九匠牛牛肉干订单.xlsx").headRowNumber(0).readHiddenRow(false).doReadAllSyncForMap(); | |
262 | +// for (Entry<Integer, List<Map<Integer, Object>>> item: map.entrySet()) { | |
263 | +// // 返回每条数据的键值对 表示所在的列 和所在列的值 | |
264 | +// LOGGER.info("读取到数据:{}", item.getKey()); | |
265 | +// LOGGER.info("读取到数据:{}", JSON.toJSONString(item.getValue())); | |
266 | +// } | |
267 | + | |
261 | 268 | // Map<Integer, List<Map<Integer, Object>>> mapListMap = EasyExcel.read(fileName).doReadAllSyncForMap(); |
262 | 269 | // Map<Integer, List<Map<Integer, Object>>> mapListMap = EasyExcel.read("D://demo.xlsx").doReadAllSyncForMap(); |
263 | 270 | // for (Entry<Integer, List<Map<Integer, Object>>> item: mapListMap.entrySet()) { |
... | ... | @@ -273,7 +280,7 @@ public class ReadTest { |
273 | 280 | for(String item: sonFileNameArr) { |
274 | 281 | try { |
275 | 282 | LOGGER.info("======文件名称:"+item+"======"); |
276 | - List<Map<Integer, String>> listMap = EasyExcel.read(dirStr+item).headRowNumber(0).readHiddenRow(false).sheet(true).doReadSync(); | |
283 | + List<Map<Integer, String>> listMap = EasyExcel.read(dirStr+item).headRowNumber(0).readHiddenRow(false).doReadSelectedSync(); | |
277 | 284 | for (Map<Integer, String> data : listMap) { |
278 | 285 | // 返回每条数据的键值对 表示所在的列 和所在列的值 |
279 | 286 | LOGGER.info(">{}", JSON.toJSONString(data)); |
... | ... | @@ -296,6 +303,6 @@ public class ReadTest { |
296 | 303 | |
297 | 304 | public static void main(String args[]) { |
298 | 305 | ReadTest rt = new ReadTest(); |
299 | - rt.readTestSynchronousRead(); | |
306 | + rt.synchronousRead(); | |
300 | 307 | } |
301 | 308 | } | ... | ... |