aspose file tools*
The moose likes JSP and the fly likes opening an existing excel file on local Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "opening an existing excel file on local" Watch "opening an existing excel file on local" New topic
Author

opening an existing excel file on local

Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144
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
Marshal

Joined: Mar 22, 2005
Posts: 42599
    
  65
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.


Ping & DNS - my free Android networking tools app
Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144
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

Joined: Dec 16, 2008
Posts: 10
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

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Dec 16, 2008
Posts: 10
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

Joined: Oct 08, 2006
Posts: 2458
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: opening an existing excel file on local