1. gsp页面head中添加
<r:require module="export"/>
2. body中适当位置添加
<export:formats formats="['csv', 'excel', 'ods', 'pdf', 'rtf', 'xml']" controller="TransATest" action="testExport"/>
3.控制器中使用
import org.codehaus.groovy.grails.commons.ConfigurationHolder
/*****
*Export,导出插件
*****/
def exportService
def testExport(){
/*******导出所有列*********/
// if(!params.max) params.max = 10
//println "testExport>>>>>>>>>>>"+User.list(params);
//
if(params?.format && params.format != "html"){
//
response.contentType = ConfigurationHolder.config.grails.mime.types[params.format]
//
response.setHeader("Content-disposition", "attachment; filename=users.${params.extension}")
//
// exportService.export(params.format, response.outputStream,User.list(params), [:], [:])
//
}
/****************/
/******导出指定列*********/
if(!params.max) params.max = 10
println "testExport>>>>>>>>>>>"+User.list(params);
if(params?.format && params.format != "html"){
response.contentType = ConfigurationHolder.config.grails.mime.types[params.format]
response.setHeader("Content-disposition", "attachment; filename=users.${params.extension}")
List fields = ["id","name", "password"]
Map labels = ["id":"Id",,"name": "Name", "password": "Password"]
/* Formatter closure in previous releases
def upperCase = { value ->
return value.toUpperCase()
}
*/
// Formatter closure
def upperCase = { domain, value ->
return value.toUpperCase()
}
Map formatters = [name: upperCase]
Map parameters = [password: "Cool password","column.widths":[0.2, 0.3, 0.5]]
exportService.export(params.format, response.outputStream, User.list(params), fields, labels, formatters,
parameters)
}
/***************/
return [ userInstanceList: User.list( params ) ]
}
/*****各参数使用参考官方文档*
*不同格式导出对应不同的参数
****/