本文介绍了在groovy中读取Excel文件最简单的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Groovy中是否有可用于读取Excel文件的warappers / utils?我正在寻找类似于Groovy SQL的函数,如下面的spock测试示例所示。我的意图是将其用于

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")
  }
}


推荐答案

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

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()
}

看看这里:

这篇关于在groovy中读取Excel文件最简单的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 22:44