jQuery in Action, 3rd edition
The moose likes Servlets and the fly likes Serving Excel Content Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Serving Excel Content" Watch "Serving Excel Content" New topic

Serving Excel Content

Andy Bowes
Ranch Hand

Joined: Jan 14, 2003
Posts: 171
Hi Guys
I know that this question has been asked and answered many times before but I have tried all sorts.
I am trying to use a servlet to generate and present a report in Excel format. I believe that the easiest way to do this is to generate the content as an HTML table and then 'fool' the client browser into opening it in Excel by setting the Content Type etc.
This servlet works fine if I set the Content Type to "text/html" but if I set the content type to "application/vnd.ms-excel" then I get an error from Excel saying that it could not open the URL.

I am totally puzzled. Has anyone got any idea what I am doing wrong ?

Andy Bowes<br />SCJP, SCWCD<br />I like deadlines, I love the whoosing noise they make as they go flying past - Douglas Adams
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
If you claim that the content type is "application/vnd.ms-excel", the browser and Excel will expect a binary stream -- the BIFF8 .xls file. You'd be better off setting the content type to "application/csv" and printing out the table as comma-separated values. Or, if you want to get really fancy, you can use Jakarta POI to generate the real thing...

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Andy Bowes
Ranch Hand

Joined: Jan 14, 2003
Posts: 171
I have also tried generating CSV content as follows :

But I still get the resource not found error when the content type is set to "text/csv". If I set the content type to "text\html" then the content is displayed correctly in the browser.
This is only a cut down version of the servlet that I am attempting as the content that I want to send to Excel will be generated by an XSL transfromation hence the reason why I want to use a HTML table or CSV rather than use POI or some other alternative.
Does anyone out there have a servlet that generates Excel files I can copy.
I agree. Here's the link: http://aspose.com/file-tools
subject: Serving Excel Content
It's not a secret anymore!