gpt4 book ai didi

java - 如何强制下载 Excel 文件?

转载 作者:行者123 更新时间:2023-12-01 10:49:02 24 4
gpt4 key购买 nike

我正在使用 Grails。我有一个下载 Excel 文件的示例代码:

XSSFWorkbook workbook = new XSSFWorkbook()
....
FileOutputStream out = new FileOutputStream(new File("C:\\excel.xlsx"))
workbook.write(out)
out.close()

此处将自动下载 Excel 文件。我希望系统提示用户在浏览器窗口中下载文件。我尝试使用以下代码:

response.setHeader("Cache-Control", "public")
response.setContentType("application/vnd.ms-excel")
response.setHeader('Content-Disposition', 'Attachment;Filename="excel.xlsx"')
ServletOutputStream outputStream = response.getOutputStream()
workbook.write(outputStream)
outputStream.flush()
outputStream.close()

这不起作用。如何实现这一目标?

提前致谢。

最佳答案

引用表格W3C

Parameter values are normally case sensitive, but certain parameters are interpreted to be case- insensitive, depending on the intended use. (For example, multipart boundaries are case-sensitive, but the "access- type" for message/External-body is not case-sensitive.)

所以,尝试改变

response.setHeader('Content-Disposition', 'Attachment;Filename="excel.xlsx"')

response.setHeader('Content-Disposition', 'attachment;filename="excel.xlsx"')

关于java - 如何强制下载 Excel 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34028838/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com