How to make excel file to be downloadable

19,606

Solution 1

You need to set the headers and content type like this:

    response.setHeader "Content-disposition", "attachment;filename=myExcel.xls"
    response.contentType = "application/vnd.ms-excel"

Then stream the content in the response.

Edit: If you need to set the content length:

    response.contentLength = 100

Content length documented in the javadoc

Solution 2

response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "Attachment;Filename=\"MyFile.xls\"");
Share:
19,606
n92
Author by

n92

Updated on June 04, 2022

Comments

  • n92
    n92 almost 2 years

    I have generated excel report and i want the file to be downloaded by the user ,so how to set the response properties (content type) .

  • Basilevs
    Basilevs almost 13 years
    As user of the site, I'd like to use benefits of content-length header too.
  • Fakipo
    Fakipo almost 4 years
    I am getting gibberish as response when I am trying this. I am getting a downloadable file but in response I am getting gibberish which does not happen when I try the same process with a pdf file.