JavaScript - HTML-Tabellendaten nach Excel exportieren
Ich versuche, HTML-Tabellen nach Excel zu konvertieren. Ich habe es mit einer JavaScript-Funktion versucht, die eine einfache Tabelle nach Excel konvertiert. Es funktioniert einwandfrei. Wenn ich mehrere Tabellen habe, wie kann ich dann alle Tabellendaten in die Excel-Datei einfügen? Hier ist, was ich versucht habe. Ich habe 2 Tabellen erstellt und einen Tabellenindex angegebentestTable
undtestTable1
.
Wie gebe ich diese 2 Tabellen-IDs beim Klicken auf die Schaltfläche an die JavaScript-Funktion weiter? Momentan wird beim Klicken auf die Schaltfläche nur die erste Tabelle nach Excel exportiert, da ich nur übergebe'testTable'
. Wie kann ich mehrere Tabellen exportieren, zB:testTable
, testTable1
in Excel?
Hier ist das 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>
Hier ist der HTML-Teil,
<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>
Bitte lassen Sie mich wissen, wie das gemacht werden kann.
Vielen Dank