[英]Set filename of Excel sheet to export from html table
I have one html table.我有一个 html 表。 Now I am exporting this table to Excel Sheet using Javascript.
现在我正在使用 Javascript 将此表导出到 Excel 表。 When this excel sheet is getting downloaded,the filename is like "download.xls".
下载此 Excel 表时,文件名类似于“download.xls”。 Now I want filename as current_date instead of "download"...Below is my javascript code:
现在我想要文件名作为 current_date 而不是“下载”...下面是我的 javascript 代码:
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 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);
}
This event is fired on a button click event. Its very important for me.
Thanks in advance.
function tableToExcel (table, name) {
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" xmlns="http://www.w3.org/TR/REC-html40"><meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></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];
})
}
if (!table.nodeType) table = document.getElementById(table)
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
var a = document.createElement('a');
a.href = uri + base64(format(template, ctx))
a.download = name+'.xls';
//triggering the function
a.click();
}
You could see: How to change the name of file while exporting data to Excel?您可以看到: 如何在将数据导出到 Excel 时更改文件名?
and this: http://www.kubilayerdogan.net/javascript-export-html-table-to-excel-with-custom-file-name/这: http : //www.kubilayerdogan.net/javascript-export-html-table-to-excel-with-custom-file-name/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.