我正在使用 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/