Easiest way to read Excel files in groovy?

前端 未结 3 918
挽巷
挽巷 2021-02-14 13:42

Are there any warappers/utils available to read Excel files in Groovy. I am looking for something similar to Groovy SQL\'s rows function as shown in below spock test ex

相关标签:
3条回答
  • 2021-02-14 14:25

    I could also recommend use Groovy Spreadsheet Builder. It available in maven repository (contrary to ExcelBuilder) and also have expressive Groovy-syntax:

    SpreadsheetQuery query = PoiSpreadsheetCriteria.FACTORY.forFile(file)                      // <1>
    
    Collection  cells = query.query {
        sheet {                                                                            
            row {                                                                           
                cell {
                    value 'B'
                }
            }
        }
    }
    
    assert cells.size() == 1
    assert cells.first().value == 'B'
    

    Or:

    Collection rows = query.query {
        sheet(name({ name.startsWith('Con') })) {
            row(1)
        }
    }.rows
    

    Documentation contains many examples. It even may write Excel files in same way!

    0 讨论(0)
  • 2021-02-14 14:31

    POI is what your after http://poi.apache.org/ its a Java Lib so you can use it from Groovy. Not sure if there are Groovy wrappers for it anywhere

    0 讨论(0)
  • 2021-02-14 14:34

    One of my fellow GUG members has created a tool for working with Excel using Apache POI in very much the same way you describe. It's not formalized into a library yet (AFAIK) but is available on his blog.

    It allows you to write code like this:

    new ExcelBuilder("customers.xls").eachLine([labels:true]) {
      new Person(name:"$firstname $lastname",
        address:address, telephone:phone).save()
    }
    

    Check it out here: http://www.technipelago.se/content/technipelago/blog/44

    0 讨论(0)
提交回复
热议问题