Groovy 加载 .csv 文件 [英] Groovy load .csv files

查看:20
本文介绍了Groovy 加载 .csv 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 grails 上的 groovy 中读取和导入 .csv 文件.我有带有数据的 .csv 文件和
需要使用用户界面导入数据库.

How to read and import .csv file in groovy on grails. I have .csv file with data and
need to import in to db using user interface .

推荐答案

在 Groovy 中处理 CSV 文件总是有不同的可能性.

There are as always different possibilities to work with CSV files in Groovy.

由于 Groovy 可与 Java 完全互操作,因此您可以使用现有的 CSV 库之一,例如OpenCSV.

As Groovy is fully interoperable with Java, you can use one of the existing CSV libararies, e.g. OpenCSV.

根据您使用的 CSV 文件的复杂性,您还可以使用 Groovy 的标准文件/字符串处理可能性:

Depending on the complexity of the CSV file you are using, you can also use the standard file/string handling possibilities of Groovy:

def sql = Sql.newInstance("jdbc:mysql://localhost:3306/mydb",
  "user", "pswd", "com.mysql.jdbc.Driver")
def people = sql.dataSet("PERSON")
new File("users.csv").splitEachLine(",") {fields ->
  people.add(
    first_name: fields[0],
    last_name: fields[1],
    email: fields[2]
  )
}

Kelly Robinson 刚刚写了一个不错的 博文 介绍了可用于在 Groovy 中处理 CSV 文件的不同可能性.

Kelly Robinson just wrote a nice blog post about the different possibilities that are available to work with CSV files in Groovy.

编辑 #2:作为 Leonard Axelsson 最近发布的 GroovyCVS 库的 1.0 版,我认为我绝对应该将其添加到选项列表中.

EDIT #2: As Leonard Axelsson recently released version 1.0 of his GroovyCVS library, I thought I should definitely add this to the list of options.

这篇关于Groovy 加载 .csv 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆