在groovy中读取Excel文件的最简单方法?

Ara*_*ram 9 java groovy spock

是否有任何warappers/utils可用于在Groovy中读取Excel文件.我正在寻找类似于Groovy SQL的函数的东西,如下面的spock测试示例所示.我的目的是在Spock测试框架中使用excel进行数据驱动测试

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")
  }
} 
Run Code Online (Sandbox Code Playgroud)

xls*_*son 15

我的一位GUG成员创建了一个使用Apache POI与Excel一起工作的工具,其方式与您描述的方式非常相似.它尚未正式进入图书馆(AFAIK),但可以在他的博客上找到.

它允许您编写如下代码:

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

请在此处查看:http://www.technipelago.se/content/technipelago/blog/44


sMo*_*ely 5

POI是您http://poi.apache.org/之后的内容,它是Java Lib,因此您可以从Groovy使用它。不确定是否在任何地方都有Groovy包装器