Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to read Excel files in groovy?

Tags:

java

groovy

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")
  }
} 
like image 235
Aravind Yarram Avatar asked May 27 '11 19:05

Aravind Yarram


People also ask

How do I read an Excel file in a groovy script?

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.

Which library is used in Soapui to work with Excel sheet?

'jxl. jar' is the jar file which supports all Excel operations, this jar supports only '. xls' format.


2 Answers

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

like image 75
sMoZely Avatar answered Sep 27 '22 02:09

sMoZely


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

like image 25
xlson Avatar answered Sep 23 '22 02:09

xlson