【发布时间】:2014-09-30 08:48:09
【问题描述】:
我有两个带有不同数据的 html 表。无法将两者都导出到 Excel 表我愿意导出单个表。
<div class="data" style="display: block;">
<table id="testTable" border="1">
<tbody>
<table style="width:300px">
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
<tbody>
</table>
</div>
<div class="data" style="display: block;">
<table id="testTable" border="1">
<tbody>
<table style="width:300px">
<tr>
<td>vsr</td>
<td>sub</td>
<td>90</td>
</tr>
<tbody>
</table>
</div>
像这样,我的表带有来自 db 的不同数据集
我的js代码是
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((s))) }
, format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
return function(table, name, filename) {
if (!table.nodeType) table = document.getElementById(resultarea)
var ctx = {worksheet: name || 'Worksheet', table: resultarea.innerHTML}
document.getElementById("dlink").href = uri + base64(format(template, ctx));
document.getElementById("dlink").download = filename;
document.getElementById("dlink").click();
}
})()
无法同时将两个表格数据导出到 Excel。请帮帮我。
【问题讨论】:
-
相同的 id 不起作用,请改用类名或更改为不同的 id,并附加/前置
1,2,3 etc.之类的东西。
标签: javascript jquery html excel dom