-
Notifications
You must be signed in to change notification settings - Fork 6
/
ExcelReader.groovy
79 lines (64 loc) · 2.11 KB
/
ExcelReader.groovy
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
@Grab('org.apache.poi:poi:3.9')
@Grab('org.apache.poi:poi-ooxml:3.9')
import org.apache.poi.ss.usermodel.WorkbookFactory
import org.apache.poi.ss.usermodel.Cell
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.ss.usermodel.DataFormatter
/**
* Groovy parser for Microsoft Excel spreadsheets.
* Based on @author Goran Ehrsson's post: http://www.technipelago.se/content/technipelago/blog/44
* Updated to handle xlsx document types, and modified to just return the string formatted value of each cell (ignore types)
*/
class ExcelReader {
def workbook
def labels
def row
ExcelReader(String fileName) {
DataFormatter dataFormatter = new DataFormatter()
Row.metaClass.getAt = { int idx ->
Cell cell = delegate.getCell(idx)
if (!cell) {
return null
}
return dataFormatter.formatCellValue(cell)
}
workbook = WorkbookFactory.create(new File(fileName))
}
def getSheet(idx) {
def sheet
if (!idx) idx = 0
if (idx instanceof Number) {
sheet = workbook.getSheetAt(idx)
} else if (idx ==~ /^\d+$/) {
sheet = workbook.getSheetAt(Integer.valueOf(idx))
} else {
sheet = workbook.getSheet(idx)
}
return sheet
}
def cell(idx) {
if (labels && (idx instanceof String)) {
idx = labels.indexOf(idx.toLowerCase())
}
return row[idx]
}
def propertyMissing(String name) {
cell(name)
}
def eachLine(Map params = [:], Closure closure) {
def offset = params.offset ?: 0
def max = params.max ?: 9999999
def sheet = getSheet(params.sheet)
def rowIterator = sheet.rowIterator()
def linesRead = 0
if (params.labels) {
labels = rowIterator.next().collect { it.toString().toLowerCase() }
}
offset.times { rowIterator.next() }
closure.setDelegate(this)
while (rowIterator.hasNext() && linesRead++ < max) {
row = rowIterator.next()
closure.call(row)
}
}
}