aspose file tools
The moose likes Java in General and the fly likes opening an excel file through Java Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "opening an excel file through Java" Watch "opening an excel file through Java" New topic
Author

opening an excel file through Java

Sekhar Kapoor
Greenhorn

Joined: Apr 24, 2007
Posts: 15
Hi,
I have a file( word or excel) in my web folder. In my application I have a hyperlink. On click of which I have to open the excel in such a way that it should allow to open or save it at my desired location through the open/save dialog box.
Can anyone help me out???
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35439
    
    9
That's what the Content-Disposition HTTP header does. Check JspAndExcel for more detail.


Android appsImageJ pluginsJava web charts
Sekhar Kapoor
Greenhorn

Joined: Apr 24, 2007
Posts: 15
Hi,
I am using the following code ::
String filePath = "C:\\Abc.xls";
File f1 = new File(filePath);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment; filename=\"Abc.xls\"");
InputStream fis = new FileInputStream(f1 );
byte[] bytearray = new byte[(int) f1.length()];
ServletOutputStream outs = response.getOutputStream();
fis.read(bytearray);
outs.write(bytearray);
outs.flush();
outs.close();
fis.close();

yet it is not working. Do you have any suggestion whjere I am going wrong. If possible can you provide some pseudo code which will help me understand the functionality better..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32830
    
    4
What happens in your exception handling?

Are you sure you have your file in the root directory on "C:\\"?
Sekhar Kapoor
Greenhorn

Joined: Apr 24, 2007
Posts: 15
The whole code I have given is within a try block and have
catch(IOException ioe){
e.printStackTrace();
}
in the catch part.
ALso the file is in C rive.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35439
    
    9
yet it is not working.

What does this mean? What do you think should be happening, and where does the observed behavior deviate from that?
Sekhar Kapoor
Greenhorn

Joined: Apr 24, 2007
Posts: 15
The page is refreshing but still neither the download box nor the excel is opening.I had given system.out so I know that the code is getting executed. yet what I need to achieve is not happening.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35439
    
    9
So what does the browser receive, then?
Sekhar Kapoor
Greenhorn

Joined: Apr 24, 2007
Posts: 15
Today I did some modifications to the code. I am now forwarding to a jsp where I have this code:

try
{
String filename = "C:\\Abc.xls";
// set the http content type to "APPLICATION/OCTET-STREAM
response.setContentType("APPLICATION/MSEXCEL");
// initialize the http content-disposition header to
// indicate a file attachment with the default filename
// "myFile.txt"
String disHeader = "Attachment;Filename=\"Abc.xls\"";
response.setHeader("Content-Disposition", disHeader);
// transfer the file byte-by-byte to the response object
File fileToDownload = new File(filename);
FileInputStream fileInputStream = new FileInputStream(fileToDownload);
int i;
/*while ((i=fileInputStream.read())!=-1)
{
out.write(i);
} */
out.write(fileInputStream.read());
fileInputStream.close();
out.close();
}
catch(Exception e)
// file IO errors
{
e.printStackTrace();
}

This is opening the the dialog box. But when I am rying to open or save the excel it is opening in one cell with some encode format........
Why is this happening and what is the solution..
Sekhar Kapoor
Greenhorn

Joined: Apr 24, 2007
Posts: 15
At last download is working for the excel. But theres one new error coming up. When I try to open the file. It says there is an error Excel repairs the error and then shows the data as a repaired file.
The error says, " Excel tried to recover your formulas and values, but some data may have been lost or corrupted."
However the excel is opening.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35439
    
    9
It's not a good idea to stream binary content from a JSP, and practically impossible to get right. I am guessing that the problem would go away if you used a servlet instead.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: opening an excel file through Java
 
Similar Threads
How do i save a file that has been generated using JSP in excel format .
excel file opening,editing and saving
How to download different files in struits dppending on the file format
javascript
IE file save as