• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in opening xlsx file when downloaded using a action class

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guyz

Is there any problem with this code??
 
Ranch Hand
Posts: 246
Firefox Browser Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im executing it in the web itself .

Its deployed in tomcat server.
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);

}

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic