-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastExcelReader.java
More file actions
354 lines (310 loc) · 12.6 KB
/
FastExcelReader.java
File metadata and controls
354 lines (310 loc) · 12.6 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
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* FastExcelReader is a lightweight and efficient utility for reading Excel files (XLSX format).
* It uses streaming to process large files without consuming excessive memory.
*
* <p>Key features: <br>
* - Handles shared strings for Excel. <br>
* - Supports configurable buffered input stream size. <br>
* - Reads sheet data row by row. <br>
* - Allows specifying a subset of column names to be read. <br>
*
* <p>Note: For parallel execution, create separate instances of FastExcelReader for each thread
* to avoid shared state issues. This ensures thread safety while processing multiple files concurrently.
*
* <p>Usage Example:
* <pre>
* {@code
* FastExcelReader reader = new FastExcelReader(4096, List.of("Name", "Age", "Country"));
* try (Stream<Map<String, String>> rows = reader.readExcel("path/to/excel.xlsx")) {
* rows.forEach(row -> {
* System.out.println("Row: " + row);
* });
* }
* }
* </pre>
* <p>
* Author: Yashwanth M
*/
public class FastExcelReader {
// XML element constants
private static final String ELEMENT_V = "v";
private static final String ELEMENT_SI = "si";
private static final String ELEMENT_T = "t";
private static final String ELEMENT_C = "c";
private static final String ELEMENT_ROW = "row";
private final Map<Integer, String> columnMap = new HashMap<>();
private final List<String> sharedStrings = new ArrayList<>();
private List<String> columnNames = null;
private int bufferedInputStreamSize = 2048;
/**
* Default constructor with default buffer size.
*/
public FastExcelReader() {
}
/**
* Constructor with configurable buffer size.
*
* @param bufferedInputStreamSize size of the buffer for reading the input stream.
*/
public FastExcelReader(int bufferedInputStreamSize) {
this.bufferedInputStreamSize = bufferedInputStreamSize;
}
/**
* Constructor with specific column names to filter.
*
* @param columnNames list of column names to be read.
*/
public FastExcelReader(List<String> columnNames) {
this.columnNames = columnNames;
}
/**
* Constructor with both buffer size and column names.
*
* @param bufferedInputStreamSize size of the buffer for reading the input stream.
* @param columnNames list of column names to be read.
*/
public FastExcelReader(int bufferedInputStreamSize, List<String> columnNames) {
this.bufferedInputStreamSize = bufferedInputStreamSize;
this.columnNames = columnNames;
}
/**
* Reads an Excel file and returns its data as a stream of maps, where each map represents a row.
*
* @param filePath path to the Excel file.
* @return a stream of rows, each represented as a map of column names to values.
* @throws Exception if an error occurs during file reading or parsing.
*/
public Stream<Map<String, String>> readExcel(String filePath) throws Exception {
try (FileInputStream fis = new FileInputStream(filePath);
BufferedInputStream bis = new BufferedInputStream(fis, bufferedInputStreamSize);
ZipInputStream zis = new ZipInputStream(bis)) {
ZipEntry entry;
// First pass: Process shared strings
while ((entry = zis.getNextEntry()) != null) {
if (entry.getName().equals("xl/sharedStrings.xml")) {
parseSharedStrings(zis);
break;
}
}
}
// Re-open the stream for the second pass
try (FileInputStream fis = new FileInputStream(filePath);
BufferedInputStream bis = new BufferedInputStream(fis, bufferedInputStreamSize);
ZipInputStream zis = new ZipInputStream(bis)) {
ZipEntry entry;
// Second pass: Process sheet data
while ((entry = zis.getNextEntry()) != null) {
if (entry.getName().startsWith("xl/worksheets/sheet")) {
return parseSheetData(zis);
}
}
}
return Stream.empty();
}
/**
* Parses shared strings XML and stores them in a list.
*
* @param stream input stream of the shared strings XML.
* @throws Exception if an error occurs during parsing.
*/
private void parseSharedStrings(InputStream stream) throws Exception {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader = factory.createXMLStreamReader(stream);
while (reader.hasNext()) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT && ELEMENT_SI.equals(reader.getLocalName())) {
sharedStrings.add(parseStringItem(reader));
}
}
}
/**
* Parses a string item from the shared strings XML.
*
* @param reader XML stream reader positioned at a shared string item.
* @return the parsed string value.
* @throws Exception if an error occurs during parsing.
*/
private String parseStringItem(XMLStreamReader reader) throws Exception {
StringBuilder sb = new StringBuilder();
while (reader.hasNext()) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT && ELEMENT_T.equals(reader.getLocalName())) {
sb.append(reader.getElementText());
} else if (event == XMLStreamConstants.END_ELEMENT && ELEMENT_SI.equals(reader.getLocalName())) {
break;
}
}
return sb.toString();
}
/**
* Parses sheet data into a stream of row maps.
*
* @param stream input stream of the sheet XML.
* @return a stream of rows, each represented as a map of column names to values.
* @throws Exception if an error occurs during parsing.
*/
private Stream<Map<String, String>> parseSheetData(InputStream stream) throws Exception {
InputStream nonClosingStream = new NonClosingInputStream(stream);
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader = factory.createXMLStreamReader(nonClosingStream);
boolean isHeaderRow = true;
List<Map<String, String>> rows = new ArrayList<>();
try {
while (reader.hasNext()) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT && ELEMENT_ROW.equals(reader.getLocalName())) {
if (isHeaderRow) {
parseHeaderRow(reader);
isHeaderRow = false;
} else {
Map<String, String> rowValues = processRow(reader);
rows.add(rowValues);
}
}
}
} finally {
reader.close();
}
return rows.stream();
}
/**
* Parses the header row to map column indices to their respective column names.
* The column map is used to match cells with their corresponding column names.
*
* <p>Note:
* - Columns are added only if no filter is provided, or if they match the specified filter.
*
* @param reader XMLStreamReader instance to parse the header row.
* @throws Exception if an error occurs while reading the XML data.
*/
private void parseHeaderRow(XMLStreamReader reader) throws Exception {
int colIndex = 0;
while (reader.hasNext()) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT && ELEMENT_C.equals(reader.getLocalName())) {
String headerValue = processCell(reader);
if (headerValue != null && !headerValue.trim().isEmpty()) {
// Add the column if no filter is provided, or if it matches the filter
if (columnNames == null || columnNames.contains(headerValue)) {
columnMap.put(colIndex, headerValue);
}
}
colIndex++;
} else if (event == XMLStreamConstants.END_ELEMENT && ELEMENT_ROW.equals(reader.getLocalName())) {
break;
}
}
}
/**
* Processes a single data row and maps cell values to their respective column names.
* Returns a map where the keys are column names and values are the corresponding cell values.
*
* <p>Note:
* - Adds null values for missing columns to ensure the map is consistent with the header.
*
* @param reader XMLStreamReader instance to parse the row.
* @return a map representing the row with column names as keys and cell values as values.
* @throws Exception if an error occurs while reading the XML data.
*/
private Map<String, String> processRow(XMLStreamReader reader) throws Exception {
Map<String, String> rowValues = new HashMap<>();
while (reader.hasNext()) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT && ELEMENT_C.equals(reader.getLocalName())) {
String cellRef = reader.getAttributeValue(null, "r");
int colIndex = getColumnIndex(cellRef);
// Process only valid columns in the column map
if (columnMap.containsKey(colIndex)) {
String columnName = columnMap.get(colIndex);
String cellValue = processCell(reader);
rowValues.put(columnName, cellValue);
}
} else if (event == XMLStreamConstants.END_ELEMENT && ELEMENT_ROW.equals(reader.getLocalName())) {
break;
}
}
// Ensure all columns have a value, even if null
for (String columnName : columnMap.values()) {
rowValues.putIfAbsent(columnName, null);
}
return rowValues;
}
/**
* Processes an individual cell to extract its value.
* Supports both shared strings and direct values.
*
* @param reader XMLStreamReader instance to parse the cell.
* @return the cell value as a string, or null if the cell is empty.
* @throws Exception if an error occurs while reading the XML data.
*/
private String processCell(XMLStreamReader reader) throws Exception {
String cellValue = null;
String type = reader.getAttributeValue(null, "t");
while (reader.hasNext()) {
int event = reader.next();
if (event == XMLStreamConstants.START_ELEMENT && ELEMENT_V.equals(reader.getLocalName())) {
String rawValue = reader.getElementText();
if ("s".equals(type)) {
cellValue = sharedStrings.get(Integer.parseInt(rawValue));
} else {
cellValue = rawValue;
}
} else if (event == XMLStreamConstants.END_ELEMENT && ELEMENT_C.equals(reader.getLocalName())) {
break;
}
}
return cellValue;
}
/**
* Converts an Excel cell reference (e.g., "A1") to a zero-based column index.
*
* @param cellRef the cell reference (e.g., "A1").
* @return the zero-based column index.
*/
private int getColumnIndex(String cellRef) {
int colIndex = 0;
for (int i = 0; i < cellRef.length(); i++) {
char ch = cellRef.charAt(i);
if (Character.isDigit(ch)) {
break; // Stop when digits are encountered
}
colIndex = colIndex * 26 + (ch - 'A' + 1);
}
return colIndex - 1; // Convert to zero-based index
}
}
/**
* A custom InputStream wrapper that prevents the underlying stream from being closed.
* This is useful for wrapping streams that need to remain open for further processing.
*/
class NonClosingInputStream extends FilterInputStream {
/**
* Constructs a NonClosingInputStream.
*
* @param in the input stream to wrap.
*/
protected NonClosingInputStream(InputStream in) {
super(in);
}
/**
* Overrides the close method to do nothing, ensuring the wrapped stream remains open.
*/
@Override
public void close() {
// Prevents the underlying InputStream from being closed
}
}