This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

opening an existing excel file on local

 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Please excuse me if I am posting this in a wrong forum.

I have a requirement of opening an existing excel sheet at a specific location on my local system on the click of a button on my jsp page.
I have written a onClickSaveExcel() which does the job of creating and writing the excel sheet on a local path using the Apache POI API.
What I really want is to open this excel sheet in a pop-up(with open,save,cancel) on the call of the onClickSaveExcel().

Can someone please guide me with the approach needed to perform the above task.

Many Thanks in advance

Best Regards

Moieen
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A JSP page can't open files that are on your local machine. You need to stream the file from the server to the browser. See the "Simple Stream" example in http://faq.javaranch.com/java/CodeBarnServlets for how to do that. Be sure to set the content type correctly for XLS files.
 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply

I will try this out.Actually I want to open the xls file in the Microsoft Excel Application and not in the web Browser.Is this possible?

Thanks again

Best Regards



 
Swapnil Gaurshettiwar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String p = FileName+"."+FileExt;

if (p.indexOf(".txt")>-1)
{
response.setContentType("text/plain");
}
else if (p.indexOf(".xls")>-1)
{
response.setContentType("application/vnd.ms-excel");
}
else if (p.indexOf(".pdf")>-1)
{
response.setContentType("application/pdf");
} else if(p.indexOf(".doc")>-1)
{
response.setContentType("application/msword");
} else if (p.indexOf(".xlsx")>-1)
{
response.setContentType("application/vnd.ms-excel");
}else if(p.indexOf(".docx")>-1)
{
response.setContentType("application/msword");
} else {
}



response.setHeader("Content-Disposition","Attachment;filename= "+p);


Attachment ATT[]= {new Attachment()};
ATT[0].setVersion("1");
ATT[0].setName(new String200(FileName));
ATT[0].setExtension(new String10(FileExt));

byte[] output = ATT1[0].getData(); // the data to be write in excel
String strop = output.toString();
String s = new sun.misc.BASE64Encoder().encode(output);

response.setContentLength(output.length+1000);
response.getOutputStream().write(output);
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't use the above code. But the point is hopefully clear, you need to set the content type and the content disposition. You need to make sure to set the correct content type so that the client knows what kind of file it can expect and thus pick the correct application for it. If you want to serve it as 'save as' or 'open in', then you need to set the content disposition to 'attachment'. If you want to serve it inline in a web page, then you need a HTML <iframe> or <object> pointing to the file(servlet) in question which sets content disposition to 'inline'.
 
Swapnil Gaurshettiwar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont know what Mr.Bauke find terrible here...Its just sample code to get the idea of how to proceed.Its according to my requirment and I have removed some part which was not required.Definitely it needs to alter as per the requirment.It will be great if Bauke suggest some good code.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You already posted a topic about that before and I already answered in there.

Under each those if/else blocks are unnecessary, those String200/String10 classes are smells, using the sun.* package is a bad practice, encoding it base64 seems superfluous, not buffering the stream is memory hogging and setting the content length +1000 is asking for trouble in client side.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic