• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 16854
  • Last Modified:

Export HTML Table to Excel

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


<script language="JavaScript">
function ExporttoExcel()
   var sOption="toolbar=yes,location=no,directories=yes,menubar=yes,";

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

   var winprint=window.open("","",sOption);

       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">')



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

<div id="'contentstart'">
  • 2
1 Solution
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...
Are you familiar with ASP... it's a much better, efficient way. I can post an example if you need me to.

etalexitAuthor Commented:
Yes, I am familiar with ASP. In fact, the application is done in ASP. Please post the code. Thanks
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%>


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

Hope This Helps,

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now