Pkira
8/9/2016 - 11:56 AM

Save html table to excel.

Save html table to excel.

 //clean and build final table
    var tab_text = "<table border='2px'><tr bgcolor='#87AFC6'>";
    var textRange; var j = 0;
    tab = document.getElementById('table_CheckSitesResult'); // id of table

    for (j = 0 ; j < tab.rows.length ; j++) {
        tab_text = tab_text + tab.rows[j].innerHTML + "</tr>";
        //tab_text=tab_text+"</tr>";
    }

    tab_text = tab_text + "</table>";
    tab_text = tab_text.replace(/<A[^>]*>|<\/A>/g, "");//remove if u want links in your table
    tab_text = tab_text.replace(/<img[^>]*>/gi, ""); // remove if u want images in your table
    tab_text = tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params
    tab_text = tab_text.replace(/(\r\n|\n|\r)/gm, "");

    //get current date
    var currentdate = new Date();
    var filename = "CheckSites_" + currentdate.getDate() + "_"
                    + (currentdate.getMonth() + 1) + "_"
                    + currentdate.getFullYear() + "_"
                    + currentdate.getHours() + "_"
                    + currentdate.getMinutes() + ".xls";

    //save table to excel
    var blob = new Blob(['\ufeff' + tab_text], {
        type: 'text/csv;charset=utf-8'
    });
    saveAs(blob,filename);