Grails .xls文件下载

时间:2012-05-15 05:48:51

标签: grails

我想在我的视图页面上有一个链接,它应该扩展为.xls文件。当我点击该链接时,我应该能够看到下载的文件。该文件应该只有.xls。我该怎么做?

1 个答案:

答案 0 :(得分:2)

您应该使用指向控制器操作的g:link,然后将.xls数据写入响应。它看起来像这样......

view.gsp

<g:link controller="foo" action="download" >Download</g:link>

然后你需要一个控制器动作......

class FooController{

    def download = {
        def file = new File("/path/to/file/somefile.xls"); //<-- you'll probably want to pass in the file name dynamically with the 'params' map    
        response.setContentType("application/excel")
        response.setHeader("Content-disposition", "attachment;filename=${file.getName()}")

        response.outputStream << file.newInputStream()

    }

}

享受!