forked from monitorjbl/excel-streaming-reader
-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathStreamingWorkbook.java
More file actions
587 lines (515 loc) · 12.2 KB
/
StreamingWorkbook.java
File metadata and controls
587 lines (515 loc) · 12.2 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
package com.github.pjfanning.xlsx.impl;
import com.github.pjfanning.xlsx.exceptions.MissingSheetException;
import com.github.pjfanning.xlsx.exceptions.ReadException;
import org.apache.poi.ooxml.POIXMLProperties;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.EvaluationWorkbook;
import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.xssf.usermodel.XSSFPictureData;
import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
import java.util.regex.Pattern;
public class StreamingWorkbook implements Workbook, Date1904Support, AutoCloseable {
private final StreamingWorkbookReader reader;
private POIXMLProperties.CoreProperties coreProperties = null;
private List<XSSFPictureData> pictures;
public StreamingWorkbook(StreamingWorkbookReader reader) {
this.reader = reader;
reader.setWorkbook(this);
}
int findSheetByName(final String name) {
final List<Map<String, String>> props = reader.getSheetProperties();
final int size = props.size();
for(int i = 0; i < size; i++) {
if(name.equalsIgnoreCase(props.get(i).get("name"))) {
return i;
}
}
return -1;
}
/* Supported */
/**
* {@inheritDoc}
*/
@Override
public Iterator<Sheet> iterator() {
return reader.iterator();
}
/**
* {@inheritDoc}
*/
@Override
public Iterator<Sheet> sheetIterator() {
return iterator();
}
@Override
public Spliterator<Sheet> spliterator() {
return reader.spliterator();
}
/**
* {@inheritDoc}
*/
@Override
public String getSheetName(int sheet) {
return reader.getSheetProperties().get(sheet).get("name");
}
/**
* Returns the index of the sheet by his name (case insensitive match)
*
* @param name the sheet name
* @return index of the sheet (0 based) or {@code -1} if not found
*/
@Override
public int getSheetIndex(String name) {
return findSheetByName(name);
}
/**
* Returns the index of the given sheet
*
* @param sheet the sheet to look up
* @return index of the sheet (0 based) or {@code -1} if not found
* @throws IllegalArgumentException if the sheet provided is not a StreamingSheet
*/
@Override
public int getSheetIndex(Sheet sheet) {
if(sheet instanceof StreamingSheet) {
return findSheetByName(sheet.getSheetName());
} else {
throw new IllegalArgumentException("Cannot use non-StreamingSheet sheets");
}
}
/**
* {@inheritDoc}
*/
@Override
public int getNumberOfSheets() {
return reader.getSheetProperties().size();
}
/**
* {@inheritDoc}
*/
@Override
public Sheet getSheetAt(final int index) {
return reader.getSheetAt(index);
}
/**
* Get sheet with the given name
*
* @param name - of the sheet
* @return Sheet with the name provided
* @throws MissingSheetException if no sheet is found with the provided <code>name</code>
*/
@Override
public Sheet getSheet(String name) throws MissingSheetException {
try {
return reader.getSheet(name);
} catch (NoSuchElementException nse) {
throw new MissingSheetException("Failed to find sheet: " + name);
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSheetHidden(int sheetIx) {
return "hidden".equals(reader.getSheetProperties().get(sheetIx).get("state"));
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSheetVeryHidden(int sheetIx) {
return "veryHidden".equals(reader.getSheetProperties().get(sheetIx).get("state"));
}
/**
* {@inheritDoc}
*/
@Override
public SpreadsheetVersion getSpreadsheetVersion() {
return SpreadsheetVersion.EXCEL2007;
}
/**
* {@inheritDoc}
*/
@Override
public void close() throws IOException {
reader.close();
}
/**
* Returns the Core Properties if this feature is enabled on the {@link com.github.pjfanning.xlsx.StreamingReader.Builder}
*
* @return CoreProperties
* @throws IllegalStateException if {@link com.github.pjfanning.xlsx.StreamingReader.Builder#setReadCoreProperties(boolean)} is not set to true
*/
public POIXMLProperties.CoreProperties getCoreProperties() {
if (reader.getBuilder().readCoreProperties()) {
return coreProperties;
} else {
throw new IllegalStateException("getCoreProperties() only works if StreamingWorking.Builder setReadCoreProperties is set to true");
}
}
void setCoreProperties(POIXMLProperties.CoreProperties coreProperties) {
this.coreProperties = coreProperties;
}
/**
* Gets all pictures from the Workbook. This approach is not stream friendly.
*
* @return the list of pictures (a list of {@link XSSFPictureData} objects.)
*/
@Override
public List<? extends PictureData> getAllPictures() {
if(pictures == null){
List<PackagePart> mediaParts = reader.getOPCPackage().getPartsByName(Pattern.compile("/xl/media/.*?"));
pictures = new ArrayList<>(mediaParts.size());
for(PackagePart part : mediaParts){
pictures.add(new XlsxPictureData(part));
}
}
return Collections.unmodifiableList(pictures);
}
/**
* {@inheritDoc}
*/
@Override
public boolean isDate1904() {
return reader.isDate1904();
}
/* Not supported */
/**
* Not supported
*/
@Override
public int getActiveSheetIndex() {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public void setActiveSheet(int sheetIndex) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public int getFirstVisibleTab() {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public void setFirstVisibleTab(int sheetIndex) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public void setSheetOrder(String sheetname, int pos) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public void setSelectedTab(int index) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public void setSheetName(int sheet, String name) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public Sheet createSheet() {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public Sheet createSheet(String sheetname) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public Sheet cloneSheet(int sheetNum) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public void removeSheetAt(int index) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public Font createFont() {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public Font findFont(boolean b, short i, short i1, String s, boolean b1, boolean b2, short i2, byte b3) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public int getNumberOfFonts() {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public int getNumberOfFontsAsInt() { throw new UnsupportedOperationException(); }
/**
* Not supported
*/
@Override
public Font getFontAt(int i) { throw new UnsupportedOperationException(); }
/**
* Not supported
*/
@Override
public CellStyle createCellStyle() {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public int getNumCellStyles() {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public CellStyle getCellStyleAt(int i) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public void write(OutputStream stream) throws IOException {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public int getNumberOfNames() {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public Name getName(String name) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public List<? extends Name> getNames(String s) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public List<? extends Name> getAllNames() {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public Name createName() {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public void removeName(Name name) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public int linkExternalWorkbook(String name, Workbook workbook) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public void setPrintArea(int sheetIndex, String reference) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public void setPrintArea(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public String getPrintArea(int sheetIndex) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public void removePrintArea(int sheetIndex) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public MissingCellPolicy getMissingCellPolicy() {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public void setMissingCellPolicy(MissingCellPolicy missingCellPolicy) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public DataFormat createDataFormat() {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public int addPicture(byte[] pictureData, int format) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public CreationHelper getCreationHelper() {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public boolean isHidden() {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public void setHidden(boolean hiddenFlag) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public void setSheetHidden(int sheetIx, boolean hidden) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public SheetVisibility getSheetVisibility(int i) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public void setSheetVisibility(int i, SheetVisibility sheetVisibility) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public void addToolPack(UDFFinder toopack) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public void setForceFormulaRecalculation(boolean value) {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public boolean getForceFormulaRecalculation() {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public int addOlePackage(byte[] bytes, String s, String s1, String s2) throws IOException {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public EvaluationWorkbook createEvaluationWorkbook() {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public CellReferenceType getCellReferenceType() {
throw new UnsupportedOperationException();
}
/**
* Not supported
*/
@Override
public void setCellReferenceType(CellReferenceType cellReferenceType) {
throw new UnsupportedOperationException();
}
}