• Post Reply Bookmark Topic Watch Topic
  • New Topic

Excel fails load to web

 
Jasmine Zhao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

We want to allow our web users to view some excel document. If they download the excel files in a zip file and open locally, they are fine. But if we send a file directly as a response to a web request, the file is not showed correctly, a lot of strange characters are displayed.

Could anyone help?
 
Preetham Chandrasekhar
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did u do a
response.setContentType = "application/vnd.ms-excel"?

Preetham
 
Jasmine Zhao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I did. I even tried to use HSSF classes to load the excel file and write to the response. The code looks something like the following:

response.setContentType("application/octet-stream");
response.setHeader( "Content-Disposition ", "inline;" + "filename=testWorkSheet.xls; ");
//reading the excel file into write out to the response output stream

Can you see anything wrong here?

Thanks,
J.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
response.setContentType("application/octet-stream");


Try:
response.setContentType("application/vnd.ms-excel");
 
Jasmine Zhao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this before, it did not work. And I tried again, it did not work either.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're going to need to stream the file to them.

As noted in previous threads from the last couple weeks, this is difficult (if not perilous) to do from a JSP. It's much easier done from a servlet.

Setting the content type and then sending the file works from a JSP if you're just sending delimited text or HTML tables. In this case you're using a 3rd party library to build a binary Excel file. I should have noticed that before telling you to change the content type.

The other option is to write the Excel file to disk on your server and use a sendRedirect to have the browser retrieve it as a static file. (yuk).
 
Jasmine Zhao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saw people saying it was possible to do it this way. I suspect something is missing from my code.

Anyway, thanks and let me know if something else comes to your mind.

Jas.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was a thread earlier this week. In it someone posted some JSP code that streamed a binary file. He managed to get it working in Tomcat 4.1 by making sure there was no space in between any of the JSP tags.

If you go that route, instead of using a servlet, there is no guarantee that it will work in other app server or in future releases of the app server you are using.

If you want to see an example of a servlet that streams binary files go to.
http://simple.servlet.com and download SimpleStream. It streams image files from the server's file system. It shouldn't be difficult to modify it to stream the Excel file you've created on the fly.

-Ben
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He managed to get it working ...


Why people insist on banging their heads against the wall like this is quite beyond me; especially when servlets are so easy to code.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
Why people insist on banging their heads against the wall like this is quite beyond me; especially when servlets are so easy to code.


Fear of javac or CLASSPATHs?
That first experience with javac leaves a bad taste in the mouths of a lot migrants from ASP/PHP/Perl... which is a shame because (as I've barked before), servlets lay the concepts of server side java out much more clearly than do JSPs.
[ January 21, 2005: Message edited by: Ben Souther ]
 
Jasmine Zhao
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ben,

You're absolutely right, Ben. I did not know how came people started to use JSP instead of Servlet. I tried using Servlet, and my tesg code works fine now. The reason I started this thread was that our legacy code was written in JSP and peope claimed that it worked in the past and it does not work any more, and I was asked to look at that. I'm going to change it into Servlet for this tiny part now.

Thanks a million and have a nice day,
Jas
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!