• Post Reply Bookmark Topic Watch Topic
  • New Topic

opening an excel file through Java  RSS feed

 
Sekhar Kapoor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what the Content-Disposition HTTP header does. Check JspAndExcel for more detail.
 
Sekhar Kapoor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens in your exception handling?

Are you sure you have your file in the root directory on "C:\\"?
 
Sekhar Kapoor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what does the browser receive, then?
 
Sekhar Kapoor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!