1

This code pretty much works flawlessly for my web application. Thanks to sampopes for the answer on this thread.

function fnExcelReport()
{
    var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>";
    var textRange; var j=0;
    tab = document.getElementById('headerTable'); // 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

    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE "); 

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
    {
        txtArea1.document.open("txt/html","replace");
        txtArea1.document.write(tab_text);
        txtArea1.document.close();
        txtArea1.focus(); 
        sa=txtArea1.document.execCommand("SaveAs",true,"Say Thanks to Sumit.xls");
    }  
    else                 //other browser not tested on IE 11
        sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  

    return (sa);
}

Basically, I have a HTML table (which is being 'populated' by handlebars and mysql with the data from the database, but we can take it out of the equation and consider a static table instead). The user can save the table in the current version however the file name is always 'download.xls' (with the exception of IE 7+). I'd like the user be able to define a file name when exporting/downloading it.

There is however a catch. I can't use any more plug-ins (think of it as a limitation)

Community
  • 1
  • 1
Xoltox
  • 11
  • 3

1 Answers1

0

You Can Use Data Table for export table data


visit link

https://datatables.net/extensions/buttons/examples/initialisation/export.html

Vivek Tamrakar
  • 502
  • 8
  • 18
  • The catch is that I don't want to use any more plug ins. I already found a few plug ins for this. Pure javascript (or jquery) only. Why? Because I've already got a hundred plus plugins on the app and I'm trying to now cut them down to increase the web page loading speed and performance. – Xoltox Jul 11 '16 at 17:19