cancel
Showing results for 
Search instead for 
Did you mean: 

Excel file download issue

shaji_chandran
Participant
0 Kudos

Hi Experts,

I am downloading an excel file which is stored at the MII server using JSP file. I am using the below code for the same. I am able to download the excel file successfully but when I open the same I get some junk value as content. I mean to say the content is not at readable format. I have tested the java code using standalone program where it worked successfully.

I  tested this JSP at MII 12.2 and MII 14.0 and both provided same result. I have attached the screenshot of excel how it looks after opening

I want to know is this a problem with Netweaver web server?. I mean the way it runs JSP. Unfortunately I could not test this on another web server

Any help on this is very much appreciated.

<%

  String filePath = "C:\\myfile.xls";

  try

  {  

  FileInputStream in = new FileInputStream(filePath);

  response.setContentType("application/vnd.ms-excel");

  response.addHeader("content-disposition","attachment; filename=" + filePath);

  int octet;

  while((octet = in.read()) != -1)

  

  out.write(octet);

  in.close();

  out.close();

  }

  catch(Exception e)

  {

  out.println("Exception occured:"+e);

  }

%>

Thanks

Shaji

Accepted Solutions (1)

Accepted Solutions (1)

former_member185280
Active Contributor
0 Kudos

You can try response.setContentType("APPLICATION/OCTET-STREAM");

Regards,
Christian

shaji_chandran
Participant
0 Kudos

Hi Christian,

I already tried the same. But no luck

Thanks

Shaji

former_member185280
Active Contributor
0 Kudos

It may not be related to the issue but I would try just passing the file name in the header instead of the whole path. e.g. just myfile.xls vs C:\\myfile.xls

Regards,
Christian

shaji_chandran
Participant
0 Kudos

Hi Christian,

Thanks for your response.

I tried that as well but same result. I get a alert message as below while opening the file.

"Excel cannot open the file Demo.xls because the file format or file extension is not valid. Verify that file has not been corrupted and that the file extension matches the format of the file"

I am trying to run the same file on another web server such as tomcat and see if the issue persist.

Thanks

Shaji

former_member185280
Active Contributor
0 Kudos

Make sure there aren't and blank spaces or newlines etc anywhere else in your jsp. 

shaji_chandran
Participant
0 Kudos

Hi Christian,

I tested jsp file at apache tomcat server and got the same result. So I think the issue is related to the code.

Thanks for your support again

Shaji

former_member185280
Active Contributor
0 Kudos

The attached jsp worked for me. A jsp is like an inside out servlet so any characters or newlines or anything outside your jsp scriptlet will be output by the server. This includes newlines after scriptlet etc. If you are trying to return a binary file with a jsp it will corrupt the file. I think this may be causing your issue.

Regards,
Christian

shaji_chandran
Participant
0 Kudos

Thanks Christian.

It works for me as well

Answers (0)