简体   繁体   English

导出到excel时设置数据类型

[英]set data type when exporting to excel

I'm using Grails 1.3.4 with the export 0.9.5 plug in. 我正在使用Grails 1.3.4以及0.9.5导出插件。

I have a formatter that I use that sets the date format to 'YYYY-MM-DD' when exporting to excel. 我有一个格式化程序,可以在导出到excel时将日期格式设置为“ YYYY-MM-DD”。 But this doesn't change the data type. 但这不会改变数据类型。 The date is exported to Excel as a string/general data type. 日期以字符串/常规数据类型导出到Excel。

def dateFormat = { domain, value ->
    if(value instanceof Date){
         return new java.text.SimpleDateFormat("yyyy-MM-dd").format(value)
    }
    return value
}

Map formatters = [ecd:dateFormat, completed:dateFormat, dateCreated:dateFormat, approvedDate:dateFormat, dpaECD:dateFormat]
exportService.export(params.format, response.outputStream,exportList, jobService.parseColNames(columns), labels, formatters, null)

Is there a way to export data and set the datatype of a column in excel so the user doesn't have to manually set the cell/column formatting to 'Date' every time after exporting? 有没有一种方法可以在Excel中导出数据并设置列的数据类型,以便用户不必在导出后每次将单元格/列格式手动设置为“日期”?

Are you sure you want to use that plugin? 您确定要使用该插件吗? It didn't work so well to me. 它对我来说效果不佳。

I've been using the JXL plugin for Grails for a while and it works perfectly. 我已经在Grails上使用JXL插件一段时间了,并且效果很好。

It even has an option to write the Excel file to the response, so that the user can directly download the file using my REST service. 它甚至可以选择将Excel文件写入响应,以便用户可以使用我的REST服务直接下载文件。

The link is: http://grails.org/plugin/jxl 链接是: http : //grails.org/plugin/jxl

Here is an example of how simple it is to create workbooks: 这是创建工作簿的简单程度的示例:

new ExcelBuilder().workbook('/path/to/test.xls') {
    sheet('SheetName') {
        cell(0,0,'Current Date')
        cell(0,1,new Date())
    }
}

Note that the cell() method has 3 parameters: column, row and value. 请注意, cell()方法具有3个参数:列,行和值。 This third parameter can be a number, string or date, and it formats it perfectly. 第三个参数可以是数字,字符串或日期,并且可以完美地格式化它。

You can find more information here . 您可以在此处找到更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM