Solved

Export HTML Table to Excel

Posted on 2006-07-18
6
16,351 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now