Grails:如何让我的列表成为MS Excel文件?

gsp分为两部分,上半部分就像searchfilter,下半部分是由Grails提供的常规列表

我在我的list.gsp中有一个模板_list.gsp。 我想创build一个button,使_list.gsp模板MS Excel文件。

但是我只想把模板放在Excel文件上。 不是网页的其余部分

我怎样才能以最简单直接的方式做到这一点? 谢谢!

如果要将列表导出为ex​​cel格式,还可以查看导出插件

http://www.grails.org/plugin/export

在你的索引方法中试试这个相当脏的技巧:

response.setHeader( 'Content-Disposition', 'attachment;filename=list.xls' ) 

说明 :诀窍在于,办公软件套件可以读取重命名为* .xls的HTML页面。 你正在做的是告诉浏览器下载一个名称的文件,而不是导航到一个普通的HTML文件。 如果你想在一个button中使用它,你应该继续使用任何HTML生成页面,然后将这个头添加到响应中。 Grails控制器中的response是全球性的。

所有你需要的是发送search数据到你的控制器,在控制器,你可以发送的内容由mschonaker解释,这是我所做的一个例子

 def report = { def reportType = params.report .... ..... //Service class that collects data based on search data passed in params def data = reportservice.execute(param) //called export plugin to generate xls file response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] response.setHeader("Content-disposition", "attachment; filename=${fileName}") exportService.export(...) } 

看看Excel导出插件。 这只支持以.xlsx格式下载。 用最简单的编码导出excel。 由于与渲染插件发生冲突,我从导出插件切换到此插件。 https://grails.org/plugin/excel-export

 response.contentType = 'application/vnd.ms-excel' response.setHeader("Content-disposition", "attachment; filename=OrderList.xlsx") new WebXlsxExporter().with { //pass the contructor with "templateFileNameWithPath" setResponseHeaders(response,new Date().format('dd-MM-yyyy_hh-mm')+"OrderList.xlsx") fillHeader(labels) add(data, fields) save(response.outputStream) } response.outputStream.flush() response.outputStream.close()}