Sto provando a convertire le tabelle HTML in Excel, ho provato con una funzione JavaScript che converte una tabella semplice in Excel, funziona perfettamente. Se ho più tabelle, come sarò in grado di aggiungere tutti i dati della tabella nel file Excel. ecco cosa ho provato Ho creato 2 tabelle e indicato l'indice di tabella testTable
e testTable1
.JavaScript - esportazione dati tabella HTML in Excel
Come passare questi 2 ID di tabella alla funzione JavaScript al clic del pulsante? proprio ora al clic del pulsante solo la prima tabella viene esportata in Excel mentre sto passando solo 'testTable'
. in che modo sarà possibile esportare più tabelle, ad esempio: testTable
, testTable1
in Excel?
Ecco il 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>
Ecco la parte 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>
prega fatemi sapere, come questo può essere fatto?
Grazie
non è possibile utilizzare il servizio web per questo problema. Funzionerà anche senza postback. – osmanraifgunes