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 example. My intention is to use this for 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, 9)")
}
def "maximum of two numbers"() {
expect:
Math.max(a, b) == c
where:
[a, b, c] << sql.rows("select a, b, c from maxdata")
}
}
You need to download and add the scriptom jar to the classpath. It is available from the maven repo here. In your groovy script import the ActiveXObject class.
'jxl. jar' is the jar file which supports all Excel operations, this jar supports only '. xls' format.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With