Groovy example how to easy and in modern DSL read and write Excel files
It utilises awesome Spreadsheet Builder library.
Please read theirs documentation and examples. It very easy and powerful.
Please look at example sample file:
Enabled | One | Two | Three |
---|---|---|---|
yes | 1 | 2 | 3 |
no | 11 | 22 | 33 |
yes | 111 | 222 | 333 |
Total: | 123 | 246 | 369 |
readExcel(
new File('/sample.xlsx').newInputStream()){ // Filter rows of interest:
'Total:' != it['Enabled']?.value?.toString()?.trim()
}
.each{row-> // Process each row. Iterator provided, so just call `each, collect`, `collectEntries` and other convenient methods
println(row.toMap())
// Access data in Map-style by bossiness names of columns:
println("Column <Two> value: ${row['Two'].value}")
}
Output will be like:
[Enabled:yes, One:1.0, Two:2.0, Three:3.0]
Column <Two> value: 2.0
[Enabled:no, One:11.0, Two:22.0, Three:33.0]
Column <Two> value: 22.0
[Enabled:yes, One:111.0, Two:222.0, Three:333.0]
Column <Two> value: 222.0
See info.hubbitus.FilterFromExcelTest.RowsWithHeader ReadXls per row. Like usage demo
for live example
processExcel(
FilterFromExcelTest.getResource('/sample.xlsx').newInputStream()
,'simple'
,new File('changed.xlsx')
,1){ // Filter rows of interest:
'Total:' != it['Enabled']?.value?.toString()?.trim()
}
.each{row-> // Process each row. Iterator provided, so just call `each, collect`, `collectEntries` and other convenient methods
row['One'] = 77 // !!!!
}
Please note, value changed and written into file changed.xlsx
automatically!
Fur build you may just run:
./gradlew shadowJar
Then just run as usual:
time java -Xmx1400m -jar build/libs/groovy-mutable-excel-1.0-SNAPSHOT-all.jar
Warn: Such run provided only for demo-purpose and use bundled in resources excel file.