wood burning stoves 2.0*
The moose likes Servlets and the fly likes HTML Table to an Excel Spreadsheet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic
Author

HTML Table to an Excel Spreadsheet

Steven Kors
Ranch Hand

Joined: Jan 30, 2002
Posts: 33
This is the code i tried to serve an HTML table to an excel spreadsheet.
<jsp:include page="/MyProject/servlet/HTMLTable" >
<jsp : param name="ft" value="9" />
<jsp : param name="cc" value="14" />
<jsp : param name="ft" value="excel" />
</jsp:include>
<%
response.setContentType("application/vnd.ms-excel");
%>
I have this above code in a jsp page and when it runs it does not work.
HTMLTable is a servlet that outputs a simple HTML Table
<TABLE BORDER=1>
<TR><TH></TH><TH>hubcaps<TH>bumbers
<TR><TH>First Day<TD>2307<TD>4706
<TR><TH>Second Day<TD>2982<TD>5104
<TR><TH>Third Day<TD>3011<TD>5220
<TR><TH>Fourth Day<TD>3055<TD>5287
</TABLE>
I can see the HTML being generated but it is not loaded into Excel. ( i see the HTMLcontent being generated on a Webclient snooping utility). I also know that this data DOES transfer into an Excel spreadsheet. If i were to hard code this table into an JSP or HTML page it laods into the Excel spreadsheet just fine.
I also tried loading this information into Excel using servlets (tried this first) but this did not work either. Here is the code;
<code>
public void sendToExcel(PrintWriter out,String table,HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
try {
out.println(table);
response.setContentType("application/vnd.ms-excel");
} catch (Exception e) {
out.println("sendToExcel Error: " + e);
e.printStackTrace();
}
}
</code>
table= the HTML table i showed above that was exported nicely into excel if it was hard coded.
out=PrintWriter
I want to crreate an HTML table from selections that the user has made. I can do this and it works just fine BUT when i try to export it into Excel i get a blank page. Excel loads fine its just empty. I tried the positioning of the setContentType directive but that didnt help. I know that the HTML table is fine because if i hard codde the table then it loads into Ecel just fine.
Oh i should say that i am using Tomcat4 (windows) and the "MyProject" directory resides in the webapps directory.
PLEASE HELP !!! =)
Thank you =)
Please help =) Hints are welcome you dont have to answer the question completely just point me in the right direction =)

PS i am sorry for cross posting this but i am getting desparate and there seems to be no one to help with my problem.. my appologies for cross posting.
[ January 30, 2002: Message edited by: Mercenary_Steel ]

Thanks,
Steven
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15962
    
  19

Your HTML is malformed, and so is the alias you're running under.
As far as the HTML goes, I think most folks 'round here use CSV form instead of HTML for feeding Excel. The alias thing, now, that's another matter. We're plain honest folk around here, and I 'spect the bartender'll want to explain things a mite to ya.
We don't normally string folks up for cross-posting on a first offense, but don't make a habit of it - we do a lot of "cross-READing" here, so even if you can't figure out the right place to ask, you stand a good chance of gettin' an answer. Even if the answer's only the right place to ask!


Customer surveys are for companies who didn't pay proper attention to begin with.
Steven Kors
Ranch Hand

Joined: Jan 30, 2002
Posts: 33
Well i certainly appreciate your patience.
As i stated in my original post, i can drop that HTML in a JSP page with the content type set to Excel and it spawns excel in the browser with no problems. Data inside as well. I create the HTML table dynamically from a servlet and include the output into a JSP page with the content type set to excel. If i put the table by itsself, as you have seen in my original post, and call that jsp page as an include, as i would call the servlet, it loads fine into Excel. I am at a loss for what to do next.
It is great putting statice tables into JSP but i want to create dynamic tables and load them into Excel. I guess i must not understand why the output of the servlet is not seen by the JSP page. I have created a webclient type application that allows for me to see the passed HTML content and when i call the servlet from the JSP page, i still see the Passed HTML but not the Excel page is not lfilled with content. its wierd i know
Thanks for your time.
PS I am not familular with csv files, could you please shed some light on the matter?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15962
    
  19

Since you also asked this question over in the JSP forum and it is actually more or less JSP-related, I'll address that in the thread over there: http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=50&t=000639
Steven Kors
Ranch Hand

Joined: Jan 30, 2002
Posts: 33
thank you
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Originally posted by Mercenary_Steel:
Well i certainly appreciate your patience.
As i stated in my original post, i can drop that HTML in a JSP page with the content type set to Excel and it spawns excel in the browser with no problems. Data inside as well. I create the HTML table dynamically from a servlet and include the output into a JSP page with the content type set to excel. If i put the table by itsself, as you have seen in my original post, and call that jsp page as an include, as i would call the servlet, it loads fine into Excel. I am at a loss for what to do next.
It is great putting statice tables into JSP but i want to create dynamic tables and load them into Excel. I guess i must not understand why the output of the servlet is not seen by the JSP page. I have created a webclient type application that allows for me to see the passed HTML content and when i call the servlet from the JSP page, i still see the Passed HTML but not the Excel page is not lfilled with content. its wierd i know
Thanks for your time.
PS I am not familular with csv files, could you please shed some light on the matter?

CSV means Comma Separated Values. It just means that you put out the data, in ASCII, with each row and column separated by commas. Open a spreadsheet and then use "Save As..." in excel to save it in CSV format and you'll see what it looks like. It's by FAR the easiest way to do something like this.
Kyle


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Steven Kors
Ranch Hand

Joined: Jan 30, 2002
Posts: 33
Kyle,
I tried the CSV format to no avail. You could go here http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=50&t=000639 and look at the problem in detail if you like
I do appreciate your input though
[ February 01, 2002: Message edited by: Steven K ]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Steven
'K' is not an acceptable last name. Please change it to conform to the naming regulations at http://www.javaranch.com/name.jsp

Thanks.

I'm closing this thread. Please post further replies to the thread in the other forum mentioned above. Please don't post the same question in multiple forums. Thanks for your co-operation.
[ February 01, 2002: Message edited by: Marilyn deQueiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
wood burning stoves
 
subject: HTML Table to an Excel Spreadsheet
 
Similar Threads
Export to excel
Using response.setContentType to switch HTML to Word
Setting content Type in JSP
HTML table to an Excel Spreadsheet
Problem in exporting the jsp in excel