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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6157114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:35:50  来源:igfitidea点击:

Easiest way to read Excel files in groovy?

javagroovyspock

提问by Aravind Yarram

Are there any warappers/utils available to read Excel files in Groovy. I am looking for something similar to Groovy SQL's rowsfunction as shown in below spock test example. My intention is to use this for data driven testing using excel in Spock test framework

是否有任何可用于读取 Groovy 中的 Excel 文件的 warappers/utils。我正在寻找类似于 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")
  }
} 

回答by xlson

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.

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

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

在这里查看:http: //www.technipelago.se/content/technipelago/blog/44

回答by sMoZely

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

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

回答by Hubbitus

I could also recommend use Groovy Spreadsheet Builder. It available in maven repository (contrary to ExcelBuilder) and also have expressive Groovy-syntax:

我也可以推荐使用Groovy Spreadsheet Builder。它在 maven 存储库中可用(与 相反ExcelBuilder)并且还具有富有表现力的 Groovy 语法:

SpreadsheetQuery query = PoiSpreadsheetCriteria.FACTORY.forFile(file)                      // <1>

Collection  cells = query.query {
    sheet {                                                                            
        row {                                                                           
            cell {
                value 'B'
            }
        }
    }
}

assert cells.size() == 1
assert cells.first().value == 'B'

Or:

或者:

Collection rows = query.query {
    sheet(name({ name.startsWith('Con') })) {
        row(1)
    }
}.rows

Documentation contains many examples. It even may write Excel files in same way!

文档包含许多示例。它甚至可以用同样的方式编写 Excel 文件!