Java – the easiest way to read Excel files in groovy?

Are there any warappers / utils that can be used to read Excel files in groovy I'm looking for something similar to groovy SQL's line function, as shown in the following Spock test example My purpose is to use it in data driven testing using Excel in Spock test framework

import groovy.sql.sql

import spock.lang.*

class DatabaseDriven extends Specification {
  @Shared sql = sql.newInstance("jdbc:h2:mem:","org.h2.Driver")

  // normally an external database would be used,// and the test data wouldn't have to be inserted here
  def setupSpec() {
    sql.execute("create table maxdata (id int primary key,a int,b int,c int)")
    sql.execute("insert into maxdata values (1,3,7,7),(2,5,4,5),(3,9,9)")
  }

  def "maximum of two numbers"() {
    expect:
    Math.max(a,b) == c

    where:
    [a,b,c] << sql.rows("select a,c from maxdata")
  }
}

Solution

One of my GUG members created a tool that works with Excel using Apache POI in a way very similar to what you described It has not officially entered the library (AFAIK), but it can be found on his blog

It allows you to write the following code:

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

View here: http://www.technipelago.se/content/technipelago/blog/44

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>