Grails _ export 插件使用
Export 插件可以将domainClass在数据库中的内容导出到excel
、pdf 、txt 等格式的文件,操作方便简单.
使用命令grails install-plugins export 安装。
使用方法:
页面中加入下面代码:
<export:resource />
<export:formats
/>
<export:formats
formats="['csv', 'excel', 'ods',
'pdf', 'rtf', 'xml']" />
如果想按条件查询结果集导出数据则需要加入params参数
<export:formats formats="['excel',
'rtf']" params="${filterParams?filterParams:params}"/>
Controller
中加入下面代码:
import org.codehaus.groovy.grails.commons.ConfigurationHolder
// Export service
provided by Export plugin
def exportService
def list = {
if(!params.max)
params.max = 10
if(params?.format
&& params.format != "html"){
response.contentType =
ConfigurationHolder.config.grails.mime.types[params.format] response.setHeader("Content-disposition", "attachment;
filename=books.${params.extension}")
exportService.export(params.format,
response.outputStream,Book.list(params), [:], [:])
}
[ bookInstanceList: Book.list( params )
]
}
自定义导出字段与显示字段名
params.max = Math.min(params.max ? params.int('max') : 10, 100)
results =AddressList.list(params);
if(params?.format && params.format != "html"){
response.contentType =
ConfigurationHolder.config.grails.mime.types[params.format];
response.setHeader("Content-disposition", "attachment;
filename=books.${params.extension}")
List fields = ["name","coName","mTel","officeTel","address","qq","msn","email","province","city"];
Map labels = ["name": "姓名","coName":"单位名称","mTel":"移动电话","officeTel":"办公电话","address":"通讯地址","qq":"QQ","msn":"MSN","email":"Email","province":"省份","city":"城市"]
exportService.export(params.format, response.outputStream,results,
fields,labels,[:], [:])
}
[addressListInstanceList:
results, addressListInstanceTotal: AddressList.count()]
其中 fields 是需要导出的字段名 labels 是需要显示的列名。
posted on 2010-03-12 17:06
Libo 阅读(1238)
评论(0) 编辑 收藏