-
Notifications
You must be signed in to change notification settings - Fork 5
/
ExcelReader.java
348 lines (295 loc) · 8.46 KB
/
ExcelReader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package JDBCExcel;
import java.util.zip.*;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
/**
* This class implements the interface between JDBC and the .XLSX file.
* @author ron
*/
public class ExcelReader {
XMLInputFactory factory;
ZipFile zipFile = null;
Map<String,InputStream> zipMap;
Map<String,String> sheetMap;
Map<String,String> styleMap;
String[] sharedStrings;
XMLStreamReader sheetReader = null;
/**
* Close the sheet reader.
* @throws Exception
*/
public void closeSheet () throws Exception {
if (sheetReader == null) return;
sheetReader.close();
sheetReader = null;
}
/**
* Close a workbook
* @throws Exception
*/
public void closeWorkbook () throws Exception {
closeSheet();
if (zipFile != null) {
zipFile.close();
zipFile = null;
}
}
/**
* Format the contents of a cell as a String
* @param value
* @param type
* @param style
* @return
* @throws Exception
*/
private String formatCell (String value,String type,String style) throws Exception {
String s = null;
if (type == null) {
if (style == null) s = value;
else {
String st = styleMap.get(style);
List<String> dateTimeFormats = Arrays.asList ( "14","22","164","165","166","167","168","169","170","171","172","173","174","175" );
if (dateTimeFormats.contains(st)) {
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
Date date = sdf.parse("1900-01-01");
Double dbl = new Double (value);
int days = (int) Math.floor (dbl);
Double secs = dbl - days;
Integer seconds = (int) Math.round(secs * 86400);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add (Calendar.DATE,days-2);
cal.add (Calendar.SECOND,seconds);
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
s = sdf.format(cal.getTime());
}
else s = value;
}
}
else if (type.equals("s")) s = sharedStrings[Integer.parseInt(value)];
else s = value;
return s;
}
/**
* Helper function to get the attributes of a node.
* @param stream
* @return
*/
private Map<String,String> getAttributes (XMLStreamReader stream) {
Map<String,String> attributes = new HashMap<>();
Integer count = stream.getAttributeCount();
for (Integer i=0; i<count; ++i) {
String name = stream.getAttributeLocalName(i);
String value = stream.getAttributeValue(i);
attributes.put (name,value);
}
return attributes;
}
/**
* Get the column reference from a cell reference.
* @param cellRef
* @return
*/
public String getColumnRef(String cellRef) {
if (cellRef == null || cellRef.equals("")) return null;
String ref = "";
char[] chars = cellRef.toCharArray();
for (char ch: chars) {
if (Character.isDigit(ch)) return ref;
ref += ch;
}
return ref;
}
/**
* Get a list of the sheets in the current workbook.
* @return
*/
public List<String> getSheetNames() {
List<String> sheetNames = new ArrayList<>();
for (String key: sheetMap.keySet()) sheetNames.add (key);
Collections.sort(sheetNames);
return sheetNames;
}
/**
* Get a stream reader for a sheet.
* @param sheet
* @return
* @throws Exception
*/
private XMLStreamReader getStreamReader (String sheet) throws Exception {
ZipEntry entry = zipFile.getEntry(sheet);
InputStream is = zipFile.getInputStream(entry);
XMLStreamReader streamReader = factory.createXMLStreamReader(is);
return streamReader;
}
/**
* Open a worksheet by name
* @param sheetName
* @throws Exception
*/
public void openSheet (String sheetName) throws Exception {
String sheetPath = sheetMap.get(sheetName);
sheetReader = getStreamReader (sheetPath);
}
/**
* Open a workbook by file name
* @param workbookFileName
* @throws Exception
*/
public void openWorkbook (String workbookFileName) throws Exception {
zipFile = new ZipFile (workbookFileName);
factory = XMLInputFactory.newInstance();
XMLStreamReader sr;
// Map sheet names to numbers.
sheetMap = new HashMap<>();
sr = getStreamReader ("xl/workbook.xml");
while (sr.hasNext()) {
sr.next();
if (sr.getEventType() == XMLStreamReader.START_ELEMENT) {
String tag = sr.getLocalName();
if (tag.equals("sheet")) {
Map<String,String> attributes = getAttributes (sr);
String sheetName = attributes.get("name");
String sheetPath = "xl/worksheets/sheet" + attributes.get("id").substring(3) + ".xml";
sheetMap.put(sheetName,sheetPath);
}
}
}
sr.close();
// Map formatting styles
styleMap = new HashMap<>();
sr = getStreamReader ("xl/styles.xml");
String xfs = "";
Integer xfCount = 0;
while(sr.hasNext()){
sr.next();
if(sr.getEventType() == XMLStreamReader.START_ELEMENT){
String tag = sr.getLocalName();
if (tag.equals("cellStyleXfs")) xfs = "cellStyle";
else if (tag.equals("cellXfs")) xfs = "cell";
else if (tag.equals("xf") && xfs.equals("cell")) {
Map<String,String> attributes = getAttributes(sr);
styleMap.put (xfCount.toString(),attributes.get("numFmtId"));
++xfCount;
}
}
}
sr.close();
// Load shared string table
sr = getStreamReader ("xl/sharedStrings.xml");
Integer i = 0;
while(sr.hasNext()){
sr.next();
Integer eventType = sr.getEventType();
if(eventType == XMLStreamReader.START_ELEMENT) {
String tag = sr.getLocalName();
if (tag.equals("sst")) {
Map<String,String> attributes = getAttributes(sr);
Integer uniqueCount = Integer.parseInt(attributes.get("uniqueCount"));
sharedStrings = new String[uniqueCount];
}
else if (tag.equals("t")) {
sr.next();
String s = sr.getText();
sharedStrings[i++] = s;
}
}
}
sr.close();
}
/**
* Get a map of reference/value for a row of cells.
* @return
* @throws Exception
*/
public Map<String,String> getRow () throws Exception {
String parentNode;
String ref = "";
String type = "";
String style = "";
String s = "";
Map<String,String> result = new LinkedHashMap<>();
while(sheetReader.hasNext()) {
sheetReader.next();
Integer eventType = sheetReader.getEventType();
if(eventType == XMLStreamReader.START_ELEMENT) {
String tag = sheetReader.getLocalName();
if (tag.equals("c")) {
Map<String,String> attributes = getAttributes(sheetReader);
ref = getColumnRef(attributes.get("r"));
type = attributes.get("t");
style = attributes.get("s");
while (!tag.equals("v")) {
sheetReader.next();
eventType = sheetReader.getEventType();
if (eventType != XMLStreamReader.CHARACTERS) tag = sheetReader.getLocalName();
}
while (eventType != XMLStreamReader.CHARACTERS) {
sheetReader.next();
eventType = sheetReader.getEventType();
}
s = sheetReader.getText();
s = formatCell (s,type,style);
result.put (ref,s);
}
}
else if (eventType == XMLStreamReader.END_ELEMENT) {
String tag = sheetReader.getLocalName();
if (tag.equals("row")) {
return result;
}
}
}
return null;
}
/**
* Get map of ref/value for the cells in a sheet
* @return
* @throws Exception
*/
public Map<String,String> getSheet () throws Exception {
String parentNode;
String ref = "";
String type = "";
String style = "";
String s = "";
Map<String,String> result = new HashMap<>();
while(sheetReader.hasNext()) {
sheetReader.next();
Integer eventType = sheetReader.getEventType();
if(eventType == XMLStreamReader.START_ELEMENT) {
String tag = sheetReader.getLocalName();
if (tag.equals("c")) {
Map<String,String> attributes = getAttributes(sheetReader);
ref = attributes.get("r");
type = attributes.get("t");
style = attributes.get("s");
while (!tag.equals("v")) {
sheetReader.next();
eventType = sheetReader.getEventType();
if (eventType != XMLStreamReader.CHARACTERS) tag = sheetReader.getLocalName();
}
while (eventType != XMLStreamReader.CHARACTERS) {
sheetReader.next();
eventType = sheetReader.getEventType();
}
s = sheetReader.getText();
s = formatCell (s,type,style);
result.put (ref,s);
}
}
}
return result;
}
}