我已经下载了将HTML表格转换为Excel的Javascript函数。它会将页面中的所有表格转换为excel,实际上我只需要一个特定的表格即可转换为excel,也可以在Button Click中使用。但下载的JavaScript代码执行页面加载本身。有人可以指导我如何使它的功能,并且还为特定的表(给定表ID)将HTML表格转换为Excel,浏览器Stucks
NameACPOEMCPUnix
NT 3.1
UnixNT 3.51
Unix95
NameACPOEMCPWindows
NT 3.1
WindowsNT 3.51
Windows95
(function(){
var cache = {};
this.tmpl = function tmpl(str, data){
// Figure out if we're getting a template, or if we need to
// load the template - and be sure to cache the result.
var fn = !/\W/.test(str) ?
cache[str] = cache[str] ||
tmpl(document.getElementById(str).innerHTML) :
// Generate a reusable function that will serve as a template
// generator (and which will be cached).
new Function("obj",
"var p=[],print=function(){p.push.apply(p,arguments);};" +
// Introduce the data as local variables using with(){}
"with(obj){p.push('" +
// Convert the template into pure JavaScript
str.replace(/[\r\t\n]/g, " ")
.split("{{").join("\t")
.replace(/((^|}})[^\t]*)'/g, "$1\r")
.replace(/\t=(.*?)}}/g, "',$1,'")
.split("\t").join("');")
.split("}}").join("p.push('")
.split("\r").join("\\'")
+ "');}return p.join('');");
// Provide some basic currying to the user
return data ? fn(data) : fn;
};
})();
var tableToExcel = (function() {
var uri = 'data:application/vnd.ms-excel;base64,',
template = '
{{for(var i=0; i{{=tables[i]}}{{ } }}',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(tableList, name) {
if (!tableList.length>0 && !tableList[0].nodeType) table = document.getElementById(table)
var tables = [];
for(var i=0; i
tables.push(tableList[i].innerHTML);
}
var ctx = {worksheet: name || 'Worksheet', tables: tables};
window.location.href = uri + base64(tmpl(template, ctx))
}
})();
tableToExcel(document.getElementsByTagName("table"),"one");
从上面的代码,我只需要“TestTable的” Excel导出。此外,当我执行这个JavaScript,浏览器中得到stucks