• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in opening xlsx file when downloaded using a action class

 
Meenakshi Prasanna
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Im using Apache POI library to generate a excel file. It works fine when i execute as stand alone application.
But in an web application it gets downloaded and when i try to open the file an error is popped up saying :

"Excel cannot open the file "workbook.xlsx" because the file format or file extension is not valid. Verify that the file has not been corrupted
and that the file extension matches the format of the file."



This is the test code which i used to integrate with the web app.
Any suggestions would be helpful.







 
Meenakshi Prasanna
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guyz

Is there any problem with this code??
 
mallikarjun dontamsetti
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you try to run application on local? try to run in web it will defiantly works i face same problem too
 
Meenakshi Prasanna
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im executing it in the web itself .

Its deployed in tomcat server.
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meenakshi,

What happens if you use xls extension instead of xlsx extension?
 
Meenakshi Prasanna
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik
its giving the same error..

"Excel cannot open the file "workbook.xlsx" because the file format or file extension is not valid. Verify that the file has not been corrupted
and that the file extension matches the format of the file"
 
Swastik Dey
Rancher
Posts: 1625
5
Android Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try with the following content type

application/vnd.ms-excel
 
Meenakshi Prasanna
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
same error even with that mime type.

Earlier i tried with jsp with application/vnd.ms-excel it works fine. But the requirement was to get the excel file in .xlsx format.
When googled i got to know abt this apache poi. As i said with this class posted it works fine with main method with servlet im not able to get it correct.

Thanks a lot for the replies.
 
Meenakshi Prasanna
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the replies.

I got the solution.. hope it helps someone who is facing same problem.
I changed the mime type to

response.setContentType("application/vnd.openxml");
and writing to servlet output stream instead of File output stream.

wb.write(response.getOutputStream());

This fixed the issue.

 
r diwan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meenakshi,
would greately appreciate if you can provide more detail on your fix
i have the same issue cannot seem to get around it

thanks for the help
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not difficult. Meenakshi's original code wrote the XLSX data to a local file on the server, instead of to the servlet's response stream. That was the wrong thing to do, since it resulted in no data being sent to the response. So changing the code to write the XLSX data to the servlet's response stream would have fixed that problem.

So if you have the same issue, i.e. if you are writing your XLSX data to a File, then apply that solution.
 
Hmbj Shah
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I too had the same issue with downloading the xlsx file.
In addition to Meenakshi 's solution (using the servlet output stream + setting the content type in the header), I had to also set the content length in the header.

response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
response.setContentType("application/vnd.openxml");

Thanks!!
 
Duverge Laurent
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi also if somebody still have the same problem I have noticed that if by mistake you type twice this:
response.setContentType("application/vnd.ms-excel");

You get the same
getOutputStream() has already been called for this response
error
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic