我想将html表导出为excel,所以我正在使用这个javascript代码:
var tableToExcel = (function () {
var tds = document.getElementsByTagName("td");
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 x:str>{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))
}
})();
首先,这段代码很适合我,但是在非常大的表中,它并不是因为uri对于window.location.href来说太长了。 他们有什么要把大表出口到Excel吗?
答案 0 :(得分:3)
我找到了一种使用FileSaver.js解决此问题的方法 1:https://github.com/eligrey/FileSaver.js/请检查以下内容
的Javascript
对于HTML表格的大数据集,这对我来说很好。
答案 1 :(得分:1)
您可以下载使用此功能的大型HTML表格。
function ExportToExcel(tableid) {
var tab_text = "<table border='2px'><tr bgcolor='#87AFC6'>";
var textRange; var j = 0;
tab = document.getElementById(tableid);//.getElementsByTagName('table'); // id of table
if (tab==null) {
return false;
}
if (tab.rows.length == 0) {
return false;
}
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, "download.xls");
}
else //other browser not tested on IE 11
//sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
try {
var blob = new Blob([tab_text], { type: "application/vnd.ms-excel" });
window.URL = window.URL || window.webkitURL;
link = window.URL.createObjectURL(blob);
a = document.createElement("a");
if (document.getElementById("caption")!=null) {
a.download=document.getElementById("caption").innerText;
}
else
{
a.download = 'download';
}
a.href = link;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} catch (e) {
}
return false;
//return (sa);
}