Grails:如何将我的列表设为 MS Excel 文件?

发布于 2024-09-27 05:14:54 字数 199 浏览 7 评论 0原文

gsp 分为两部分,顶部部分就像一个搜索过滤器,底部部分是 Grails 提供的常规列表

。我的 list.gsp 中有一个模板 _list.gsp。我想创建一个按钮,使 _list.gsp 模板成为 MS Excel 文件。

但我只想将模板放在 Excel 文件中。不是页面的其余部分

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

gsp is divided into two, the top part is like a search filter, and the bottom part is the regular list that is provided by Grails

I have a template _list.gsp in my list.gsp. And I wanna create a button to make that _list.gsp template a MS Excel file.

But I want only the template to be on the Excel file. Not the rest of the page

How can I do this in the simplest and direct way possible?? Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

彩扇题诗 2024-10-04 05:14:54

如果您想将列表导出为 Excel 格式,您还可以查看导出插件

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

you can also take a look at the export plugin if you want to export the list to excel format

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

青衫儰鉨ミ守葔 2024-10-04 05:14:54

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

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

说明:这个技巧是重命名为 *.xls 的 HTML 页面可以被办公软件套件读取。您所做的就是告诉浏览器下载具有该名称的文件,而不是导航到常规 HTML 文件。如果您想在按钮中使用它,您应该像处理任何 HTML 生成页面一样继续,然后将此标头添加到响应中。 response 对于 Grails 控制器中的操作来说是全局的。

Try this quite dirty trick in your index method:

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

Explanation: the trick is that an HTML page renamed as *.xls can be read by office software suites. What you're doing is telling the browser to download a file with that name, instead of navigating to a regular HTML file. If you want to use this in a button, you should proceed as with any HTML generating page, and then add this header to the response. response is global to actions in Grails controllers.

白云悠悠 2024-10-04 05:14:54

您所需要做的就是将搜索数据发送到您的控制器,在控制器中您可以按照 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(...)
}

All you need is to send search data to your controller, in the controller you can send content as explained by mschonaker, this is an example of what I did

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(...)
}
み格子的夏天 2024-10-04 05:14:54

看一下 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()}

Take a look at the Excel-export plugin. This has support for downloading only in .xlsx format. Exports an excel with the simplest coding.
I switched to this plugin from export plugin since had a clash with rendering plugin.
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()}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文