Solved

Export HTML Table to Excel

Posted on 2006-07-18
6
16,537 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

688 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