我正在尝试将 HTML 表格转换为 Excel,我已经尝试使用 JavaScript 函数将简单表格转换为 Excel,它工作正常。如果我有多个表格,我将如何将所有表格数据添加到 Excel 文件中。这是我试过的。我创建了 2 个表并给定了表索引 testTable
和 testTable1
。
我如何在单击按钮时将这 2 个表 ID 传递给 JavaScript 函数?现在单击按钮时,只有第一个表导出到 Excel,因为我只传递了 'testTable'
。我如何才能将多个表(例如:testTable
、testTable1
)导出到 Excel 中?
这是 JavaScript:
<script>
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>
<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]; }) }
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))
}
})()
</script>
这是 HTML 部分,
<table id="testTable">
<thead>
<tr>
<th>Name</th>
<th>ACP</th>
<th>OEMCP</th>
<th>Unix<br>
NT 3.1</th>
<th>Unix<br>
NT 3.51</th>
<th>Unix<br>
95</th>
</tr>
</thead>
</table>
<table id="testTable1">
<thead>
<tr>
<th>Name</th>
<th>ACP</th>
<th>OEMCP</th>
<th>Windows<br>
NT 3.1</th>
<th>Windows<br>
NT 3.51</th>
<th>Windows<br>
95</th>
</tr>
</thead>
</table>
请告诉我,这是如何做到的?
谢谢
最佳答案
我推荐另一种格式化方法。 John Resig 微型模板是一个非常好的简单工具,可以满足您的需要。 ( ejohn microtemplating )
(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;
};
})();
使用起来非常简单。这不仅允许在 HTML 之间显示变量,还允许执行 JavaScript 代码
您的模板字符串需要进行一些修改才能使用此微模板。
{{for(var i=0; i<tables.length;i++){ }}
<table>
{{=tables[i]}}
</table>
{{ } }}
最后只需要选择所有出现在你的例子中的表
document.getElementsByTagName("table");
你可以看到它是如何工作的http://jsfiddle.net/Scipion/P8rpn/1/
关于JavaScript - 将 HTML 表格数据导出到 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15224002/