Manjunath
Manjunath

Reputation: 31

How do I force download an excel file?

I am using Grails. I have a sample code to download an excel file:

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

Here the excel file will be downloaded automatically. I would want system to prompt the user to download the file in the browser window. I tried using the below code:

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()

Which doesn't work. How to achieve this?

Thanks in advance.

Upvotes: 0

Views: 1900

Answers (1)

Amanuel Nega
Amanuel Nega

Reputation: 1977

Quoted form 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.)

There for, try changing

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

to

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

Upvotes: 1

Related Questions