How to export a table in Excel

I have a table where the data is populated dynamically, and I would like to export this table in .xlsm or .xls format using JavaScript. It should work in IE

This is my table:

function Table(x) {
var statusCol = "";
var table = '<table><tr><th>First Name</th><th>Surname</th><th>Surname</th></tr>';
var ID = 0;
for (var i=0;i<10;i++) {

    var row = "<tr age='"+(12+i)+"' class='staff-row' id='" +"ColId"+i + "'>";

    row += '<td>' + "FName" + '</td>';
    row += '<td>' + "SName" + '</td>'
    row+="</tr>"
    ID++;
    table += row;
}
table += '</table>';
$('#DisplayTable').html(table);
$('#DisplayTable').tooltip({
    'show': true,
    'selector':'.staff-row',
    'placement': 'bottom',
    'title': function(event){
        var $this=$(this);
        var tds=$this.find('td');            
        return $(tds[0]).text()+" "+$(tds[1]).text()+" age: "+$this.attr("age");
    },        
});

    <button type="button" class="btn btn-default export">

I did a few online searches, and all I found was this, but I don't understand some of these code examples, for example. How to link my table with this? and how would I embed this code so that when the user clicks on the button, the .xlsm file is exported . I mean, I have a function onClick, but it does nothing?

$(document).ready(function () {
        $('.export').on('click', function () {
            var tableToExcel = (function () {
                var uri = 'data:application/vnd.ms-excel;base64,'
                  , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"><head><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</table></body></html>'
                  , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
                  , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
                return function (table, name) {
                    if (!table.nodeType) table = document.getElementById(table)
                    var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
                    window.location.href = uri + base64(format(template, ctx))
                }
            })()
        });
    });

Here is the JSFiddle: JSFIDDLE DEMO

This is what other people did. I also looked at Link. JSFIDDLE DEMO

+3
1

Excel - JQuery DataTables Table Tools. , , , , , Excel, PDF, CSV, .

, http://datatables.net/release-datatables/extras/TableTools/index.html

0

All Articles