File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Opening an xlsx Excel file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Opening an xlsx Excel file" Watch "Opening an xlsx Excel file" New topic

Opening an xlsx Excel file

gendhe ranjith kumar

Joined: Mar 28, 2011
Posts: 20

i am using the following code to download xlsx file from server.

String resultXLS="file downloading from jsp.xlsx";
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8");
response.setHeader("content-disposition", "attachment; filename=\"" + resultXLS + "\"");

File file = new File(filepath);
FileInputStream fileIn = new FileInputStream(file);
OutputStream outExcel = response.getOutputStream();

byte[] outputByte = new byte[8192];
//copy binary contect to output stream
while(, 0, 8192) != -1)
outExcel.write(outputByte, 0, 8192);



the file generated. but i am getting some error messages.

1."excel found unreadable content. do you want to recover from it".

2."the file cannot opened by Microsoft excel . do you want to search on online for converter to open the file".

any help on this will be appreciated.

Thanks & regards

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41089
You can't handle binary data with JSPs; you need to write a servlet instead.

Ping & DNS - my free Android networking tools app
gendhe ranjith kumar

Joined: Mar 28, 2011
Posts: 20

thanks for your response.

the above code is working fine in case of 2003 excel.

it is fails in excel 2007.

thanks & regards

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41089
Then you got lucky (and the old Excel file format may be more forgiving than the new one). But I repeat: using JSPs to handle binary data will fail eventually. You're better off getting it right from the start than to suffer later.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Opening an xlsx Excel file
Similar Threads
Problem on opening .XLSX file
xlsx file name problem in internet explorer
displaying jsp filename to downloading file in internet explorer
Forwarding after response committed
problems in downloading epub files