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

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

问题描述

如何在grails中的groovy中读取和导入.csv文件。我有.csv文件与数据和\\ b $ b需要使用用户界面导入到db。

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 .

提前感谢,

srinath

thanks in advance,
srinath

推荐答案

在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只是写了一个不错的博客文章<

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,您可以在Groovy中使用CSV文件
最近发布的1.0版本的他的GroovyCVS库,我想我应该绝对将此添加到选项列表中。

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天全站免登陆