项目作者: Hubbitus

项目描述 :
Groovy example how to easy and in modern DSL read and write Excel files
高级语言: Groovy
项目地址: git://github.com/Hubbitus/groovy-mutable-excel.git
创建时间: 2020-06-02T08:43:15Z
项目社区:https://github.com/Hubbitus/groovy-mutable-excel

开源协议:

下载


Demo how to read and write Excel files in simple Groovy DSL syntax

It utilises awesome Spreadsheet Builder library.

Please read theirs documentation and examples. It very easy and powerful.

Threat Excel like Map for reading (with keys in header column)

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
  1. readExcel(
  2. new File('/sample.xlsx').newInputStream()){ // Filter rows of interest:
  3. 'Total:' != it['Enabled']?.value?.toString()?.trim()
  4. }
  5. .each{row-> // Process each row. Iterator provided, so just call `each, collect`, `collectEntries` and other convenient methods
  6. println(row.toMap())
  7. // Access data in Map-style by bossiness names of columns:
  8. println("Column <Two> value: ${row['Two'].value}")
  9. }

Output will be like:

  1. [Enabled:yes, One:1.0, Two:2.0, Three:3.0]
  2. Column <Two> value: 2.0
  3. [Enabled:no, One:11.0, Two:22.0, Three:33.0]
  4. Column <Two> value: 22.0
  5. [Enabled:yes, One:111.0, Two:222.0, Three:333.0]
  6. Column <Two> value: 222.0

See info.hubbitus.FilterFromExcelTest.RowsWithHeader ReadXls per row. Like usage demo for live example

Read and update (write) file content!

  1. processExcel(
  2. FilterFromExcelTest.getResource('/sample.xlsx').newInputStream()
  3. ,'simple'
  4. ,new File('changed.xlsx')
  5. ,1){ // Filter rows of interest:
  6. 'Total:' != it['Enabled']?.value?.toString()?.trim()
  7. }
  8. .each{row-> // Process each row. Iterator provided, so just call `each, collect`, `collectEntries` and other convenient methods
  9. row['One'] = 77 // !!!!
  10. }

Please note, value changed and written into file changed.xlsx automatically!

Development

Fur build you may just run:

  1. ./gradlew shadowJar

Then just run as usual:

  1. 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.