aspose file tools
The moose likes Other Open Source Projects and the fly likes Problem in opening xlsx file when downloaded using a action class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Reply Bookmark "Problem in opening xlsx file when downloaded using a action class" Watch "Problem in opening xlsx file when downloaded using a action class" New topic
Author

Problem in opening xlsx file when downloaded using a action class

Meenakshi Prasanna
Greenhorn

Joined: May 29, 2012
Posts: 6
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

Joined: May 29, 2012
Posts: 6
Hi Guyz

Is there any problem with this code??
mallikarjun dontamsetti
Ranch Hand

Joined: Mar 18, 2011
Posts: 233

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

Joined: May 29, 2012
Posts: 6
Im executing it in the web itself .

Its deployed in tomcat server.
Swastik Dey
Ranch Hand

Joined: Jan 08, 2009
Posts: 1195

Meenakshi,

What happens if you use xls extension instead of xlsx extension?


Swastik
Meenakshi Prasanna
Greenhorn

Joined: May 29, 2012
Posts: 6
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
Ranch Hand

Joined: Jan 08, 2009
Posts: 1195

Try with the following content type

application/vnd.ms-excel
Meenakshi Prasanna
Greenhorn

Joined: May 29, 2012
Posts: 6
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

Joined: May 29, 2012
Posts: 6
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

Joined: Aug 02, 2012
Posts: 1
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
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

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

Joined: Apr 19, 2010
Posts: 3
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!!


HS
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Problem in opening xlsx file when downloaded using a action class
 
Similar Threads
Generate Excel report in java
JSP to Excel Conversion
Rendering Excel spreadsheet in JSP OR via CSV File Download
apache pio?
How to make POI HSSF support Chinese Encoding?