Forums Register Login

Problem in opening xlsx file when downloaded using a action class

+Pie Number of slices to send: Send
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.







+Pie Number of slices to send: Send
Hi Guyz

Is there any problem with this code??
1
+Pie Number of slices to send: Send
are you try to run application on local? try to run in web it will defiantly works i face same problem too
+Pie Number of slices to send: Send
Im executing it in the web itself .

Its deployed in tomcat server.
1
+Pie Number of slices to send: Send
Meenakshi,

What happens if you use xls extension instead of xlsx extension?
+Pie Number of slices to send: Send
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"
1
+Pie Number of slices to send: Send
Try with the following content type

application/vnd.ms-excel
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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!!
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
i cant download my excel file in netbeans
this type of error comes...please help me to solve this
"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 my codeing
<%@page import="java.io.FileInputStream"%>

<%@ page contentType="application/vnd.ms-excel" pageEncoding="ISO-8859-1"%>
<%response.setHeader("Content-Disposition", "attachment;filename=paxsearchlist.xlsx"); %>


<%    
try
             
{
   
  String fil=request.getParameter("fname");
 
System.out.println(fil);
 
// ServletContext context = getServletContext();

//
String dirName =context.getRealPath("/staffs/");
   
//            System.out.println(dirName);
//
String filepath=dirName;
//
System.out.println(filepath);

//response.setContentType("text/html");  

 


 

//FileInputStream fileInputStream = new FileInputStream(filepath +"/"+ fil);  

//System.out.println(fileInputStream);
//int i;

//while((i=fileInputStream.read())!=-1)
//
{

//    System.out.println(i);

//out.write(i);
//
}

//.close();//
out.close();

   
   
   
   
 

ServletContext context = getServletContext();

String path=context.getRealPath("\\.xlsx\\");

//String filepath = "e:\\";
 
response.setContentType("/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
 
response.setHeader("Content-Disposition","attachment; filename=\"" + fil + "\"");
 
java.io.FileInputStream fileInputStream = new java.io.FileInputStream(path +"\\"+ fil);
 
 
int i;
 
while ((i=fileInputStream.read()) != -1)
{
   out.write(i);
 
}
 
 fileInputStream.close();


} catch (Exception e)
{

System.out.println(e);

}

How do they get the deer to cross at the signs? Or to read this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 38281 times.
Similar Threads
Rendering Excel spreadsheet in JSP OR via CSV File Download
Generate Excel report in java
JSP to Excel Conversion
apache pio?
How to make POI HSSF support Chinese Encoding?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 10:02:32.