Solved

Export HTML Table to Excel

Posted on 2006-07-18
6
16,402 Views
Last Modified: 2012-05-05
Dear Experts,

How to export a html table within a dive tag to excel??

Below is what I have so far but it is not working properly.
I expect by clicking on "export to excel" hyperlink, a separate browser window will open up the excel application with the information in the table.

Thanks for any help in advance

etalexit

---------------------------------------------------------------------------------------------------------------------
<html>
<head>
<script language="JavaScript">
function ExporttoExcel()
{
   var sOption="toolbar=yes,location=no,directories=yes,menubar=yes,";
       sOption+="scrollbars=yes,width=750,height=600,left=100,top=25";

   var sWinHTML = document.getElementById('contentstart').innerHTML;

   var winprint=window.open("","",sOption);
       winprint.document.open();
       winprint.document.write('<html><head>')

       winprint.document.write('<meta http-equiv="Content-Type" content="application/vnd.ms-excel">')
         winprint.document.write('<meta http-equiv="Content-disposition": attachment; filename="file.xls">')
         winprint.document.write('</head><body>')

       winprint.document.write(sWinHTML);
       winprint.document.write('</body></html>');
       winprint.document.close();
       winprint.focus();
}

</script>
</head>

<body>
<a href="javascript:ExporttoExcel();" >Export to Excel</a>

<div id="'contentstart'">
<table>
<tr><td>testing</td></tr>
</table>
</div>
</body>
</html>
0
Comment
Question by:etalexit
  • 2
6 Comments
 
LVL 5

Expert Comment

by:koolie
ID: 17135288
This probably isnt what you are looking for, but the easiest way to do this is with Internet Explorer ( and a computer that has Excel installed on it ).  You can right-click on a page and export to excel and insert a html table into an excel page...
0
 
LVL 6

Expert Comment

by:davecestria
ID: 17137765
Are you familiar with ASP... it's a much better, efficient way. I can post an example if you need me to.

Dave
0
 

Author Comment

by:etalexit
ID: 17137830
Dave,
Yes, I am familiar with ASP. In fact, the application is done in ASP. Please post the code. Thanks
etalexit
0
 
LVL 6

Accepted Solution

by:
davecestria earned 500 total points
ID: 17137923
The following code allows you to display your table as normal in IE, then if the page is loaded with ?action=export as the querystring it will prompt you to save or open the exprted excel document.

If you only wish to hide parts of your page just use:

<%if Request.QueryString("action") <> "export" then%>
Content Which You Do Not Want Seen When You Export
<%end if%>

----CODE:

<%
if Request.QueryString("action") = "export" then
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "attachment;filename=Export.xls"
else
Response.Write "<a href=""?action=export"">Export</a><br><br>"
end if
%>
<table>
<tr><td>Your HTML Table Here</td></tr>
</table>




Hope This Helps,


Dave
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
The viewer will learn how to dynamically set the form action using jQuery.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question