我试图将HTML表格转换为Excel,我尝试使用将简单表格转换为Excel的JavaScript函数,它工作正常。如果我有多个表格,我将如何将所有表格数据添加到Excel文件中。这是我的尝试。我创建了2个表格并给出了表格索引testTable
和testTable1
。JavaScript - 将HTML表格数据导出到Excel中
如何通过点击按钮将这两个表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>
请让我知道,这可怎么办呢?
感谢
不能使用Web服务这个问题。它也将工作,无需回传。 – osmanraifgunes 2013-03-05 12:50:24