Export table data to Excel using jQuery

You must have exported table data to excel using server side language like ASP.NET, PHP, JSP etc. But how about exporting HTML table data to Excel using jQuery.


To export the table, use window.open() method to open Excel application and pass the table's content.
window.open(MIMEtype,replace);
  • MIMEtype: Optional. The type of document you are writing to. Default value is "text/html".
  • replace: Optional. If set, the history entry for the new document inherits the history entry from the document which opened this document.
Below jQuery code will export the table data to Excel.
$("#btnExport").click(function(e) {
    window.open('data:application/vnd.ms-excel,' + $('#dvData').html());
    e.preventDefault();
});​
See result below



If your table HTML contain special characters, then before sending the html to excel it must be encoded.
$("#btnExport").click(function(e) {
    window.open('data:application/vnd.ms-excel,' + encodeURIComponent($('#dvData').html()));
    e.preventDefault();
});​
See Complete Code
Note: This solution will not work in IE.

The above solution is not going to work with IE. To make it work in IE, you need to set response header. If you are using ASP.NET then read Export table to Excel using jQuery in IE.
Response.AddHeader("Content-Disposition", "attachment;filename=download.xls");
You can find more details about this issue at
Link 1
Link 2

Feel free to contact me for any help related to jQuery, I will gladly help you.
SHARE

Nothing shocks me, I'm a Software Engineer. And I am not young enough to know everything. I live in World Wide Web and from there take care of this website. This website communicates about my work, learning and experience. I believe life is short, and it is for loving, sharing, learning and connecting. So lets connect..