Abhijeet Deshmukh

Abhijeet Deshmukh

  • NA
  • 16
  • 24.5k

Set filename of Excel sheet to export from html table

Jul 29 2015 3:57 AM
I have one html table. Now I am exporting this table to Excel Sheet 
using Javascript. When this excel sheet is getting downloaded,the
filename is like "download.xls". Now I want filename as current_date
instead of "download"...Below is my javascript code:
 
function write_to_excel() {
var tab_text = "<table border='2px'><tr>";
var textRange; var j = 0;
tab = document.getElementById('tt1'); // id of table
tab1 = document.getElementById('testTable'); // id of table
for (i = 0; i < tab.rows.length; i++) {
tab_text = tab_text + tab.rows[i].innerHTML + "</tr>";
//tab_text=tab_text+"</tr>";
}
for (j = 0; j < tab1.rows.length; j++) {
tab_text = tab_text + tab1.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 a = document.createElement('a');
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);
}
 

Answers (3)