2011-05-27 134 views
3

Groovy中是否有可用於讀取Excel文件的warappers/utils?我正在尋找類似於Groovy SQL的函數,如下面的spock測試示例所示。我的目的是用這個data driven testing using excel in Spock test framework在groovy中讀取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") 
    } 
} 

回答

10

我的一位GUG成員創建了一個使用Apache POI以非常類似於您描述的方式使用Excel的工具。它尚未正式化爲圖書館(AFAIK),但可在其博客上找到。

它可以讓你寫這樣的代碼:

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

看看這裏:http://www.technipelago.se/content/technipelago/blog/44

+0

爲了使事情的人更容易一些,我轉身戈蘭的代碼放到一個GitHub庫。我也修改它以使用Grab的依賴關係,使用xlsx文件,併爲每個單元格返回字符串。使用細胞類型給我帶來麻煩,因爲我需要做的事情。 https://github.com/dtanner/groovy-excel-reader – 2014-03-27 14:53:01

4

POI是什麼你http://poi.apache.org/後的一個Java庫,以便可以從Groovy中使用它。不知道是否有任何地方的Groovy包裝

相關問題