1

I am trying to generate excel using JSP, but generated file is not getting opened directly. If I save this file and try to open then it is getting opened but if I try to open it directly then getting below error:

The file is corrupt and cannot be opened.

I am using below specification for response header:

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment;filename="+"fileName.xls");

I also tried:

response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

Any help is appreciable. Thanks in advance.

DimaSan
  • 12,264
  • 11
  • 65
  • 75
  • Are you really generating an xls file (binary proprietary format from Microsoft) using JSP, or simply a Comma Separated Values file? – Serge Ballesta Dec 13 '16 at 08:55
  • I am trying to generate excel file and jsf is used to format data in row and column format for excel. – Amit Sharma Dec 13 '16 at 09:03
  • Data formatted in row and column is **not** in excel format but in CSV. In that case you should declare `response.setContentType("text/csv");` as explained in that [other SO post](http://stackoverflow.com/a/7076079/3545273) – Serge Ballesta Dec 13 '16 at 09:08
  • @SergeBallesta, I tried using content type as "text/csv" but it is not working. Still same issue. – Amit Sharma Dec 13 '16 at 09:20
  • Did you associate this file extension in your browser to be opened with the right application? – code_angel Dec 13 '16 at 10:29
  • No, I am not associating this file extension in browser. Extension is concatenated with file name. Since extension is .xls so it should open in excel file. Also table format is being used to represent data. – Amit Sharma Dec 13 '16 at 11:05
  • If its is a csv file, use `.csv` as extension... `.xls` is a different format. – Serge Ballesta Dec 13 '16 at 12:33

0 Answers0