I have a page where multiple HTML tables are there...I need to download all the HTML tables to Microsoft Excel. With the current coding I have I can download the first table but I need to download all the tables.
This is the JavaScript which I have used:
<script type="text/javascript">
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>
And this is the portion of the PHP where the table is populated:
<div id="render_me">
<table width='100%' id="testTable">
<col width="15%" />
<col width="10%" />
<col width="25%" />
<col width="15%" />
<col width="10%" />
<col width="15%" />
<col width="10%" />
<tr>
<th><?php echo $b; //row id ?></th>
<th class='forcedWidth1'>Merk</th>
<th class='forcedWidth1'>Artikel naam</th>
<th>EAN</th>
<th>Prijs Kieskeurig.nl</th>
<th>Verschil Kieskeurig.nl</th>
<th>RAL Prijs</th>
</tr>
<?php
while($row=mysql_fetch_object($sql)):?>
<tr>
<td width:'90px'></td>
<td class='forcedWidth1'><?php echo $row->merk; //row id ?></td>
<td class='forcedWidth1'><?php echo (utf8_decode($row->OM)); //row id ?></td>
<td class='forcedWidth'><?php echo(utf8_decode("<a href=".$row->TW_URL.">".$row->EAN."</a>")); ?></td>
<td class='forcedWidth'><?php $num2 = $row->PRICE; echo number_format($num2,2,'.', ''); ?></td>
<td class='forcedWidth'><?php $num6 = $num2 - $row->RAL; echo number_format($num6, 2, '.', ''); // row first name ?></td>
<td class='forcedWidth'><?php echo $row->RAL; // row first name ?></td>
</tr>
<?php endwhile;?>
</table>
<?php } }?>
<?php }?>
<button id="btnExport" class="button2"></button> <button onclick="demoPDF()" class="button1">
</button>
</div>
<input type="button" onclick="tableToExcel('testTable', 'W3C Example Table')" value="Export to Excel">
Any help is very much appreciated.