Ich versuche, HTML-Tabellen in Excel zu konvertieren, habe ich mit einer JavaScript-Funktion versucht, die eine einfache Tabelle in Excel konvertiert, funktioniert es gut. Wenn ich mehrere Tabellen habe, wie kann ich alle Tabellendaten in die Excel-Datei einfügen. Hier ist, was ich versucht habe. Ich habe 2 Tabellen erstellt und Tabellenindex testTable
und testTable1
gegeben.JavaScript - HTML-Tabellendaten in Excel exportieren
Wie werde ich diese 2 Tabelle IDs auf die JavaScript-Funktion auf einen Klick auf die Schaltfläche übergeben? Gerade jetzt beim Klicken auf die Schaltfläche wird nur die erste Tabelle in Excel exportiert, da ich nur 'testTable'
übergebe. 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 dies getan werden kann?
Dank
Können Sie nicht Web-Service für dieses Problem verwenden. Es funktioniert auch ohne Postback. – osmanraifgunes