Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Opening an xlsx Excel file

 
gendhe ranjith kumar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

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(fileIn.read(outputByte, 0, 8192) != -1)
{
outExcel.write(outputByte, 0, 8192);
}

fileIn.close();
outExcel.flush();
outExcel.close();

%>



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

Ranjith
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't handle binary data with JSPs; you need to write a servlet instead.
 
gendhe ranjith kumar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

thanks for your response.

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

it is fails in excel 2007.

thanks & regards

Ranjith.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic