1
回答
js中表格转为excel文件如何实现多sheet形式?

如下代码:

var tableToExcel = (function() {
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"><head><!--[if gte mso 9]><?xml version="1.0" encoding="UTF-8" standalone="yes"?><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>'+
'</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table style="vnd.ms-excel.numberformat:@">{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) {
debugger;
return c[p];
})
}
return function(table, name) {
if(!table.nodeType) table = document.getElementById(table)
var ctx = {
worksheet1: 'Worksheet1',
worksheet2: 'Worksheet2',
table: table.innerHTML
}
//window.location.href = uri + base64(format(template, ctx))
var link = document.createElement("a");
link.href = uri + base64(format(template, ctx));


link.style = "visibility:hidden";
link.download = name + ".xls";


document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
})()
在调用函数tableToExcel(tableid,excelName)时可实现将表格转为excel文件,但如何在此基础上实现传入多个表格时分别转为excel表格的多个sheet呢?不是此方式,可以在前台js实现将多个表格转为多sheet形式excel文件的也请指导下。(说服务端实现的就不用了,强制要求前台实现,感谢!)

<无标签>
举报
顶部