如何使用JavaScript将多个html表导出为csv文件?

我发现这个如何使用JavaScript将单个表导出到excel文件中,并且对于一个表格来说它是完美的:

HTML表格到Excel的Javascript

但是,我有多个表,我想导出到一个CSV文件。 我怎么能修改该代码来完成这个?

我尝试将“getElementById”更改为“getElementByClass”,并为这些表添加了相同的类名,但是excel输出只是表示“未定义”

这是我尝试的代码

<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, filename) { if (!table.nodeType) table = document.getElementByClass(table) var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML } document.getElementById("dlink").href = uri + base64(format(template, ctx)); document.getElementById("dlink").download = filename; document.getElementById("dlink").click(); } })()</script> <a id="dlink" style="display:none;"></a> <input type="button" onclick="tableToExcel('alltables', 'alltables', 'myfile.xls')" value="Export to Excel"> print "<table id='vmax' class='alltables'>"; print "<caption>VMAX - 3720</caption>"; print "<tr>"; print "<th>Date</th>"; print "<th>Type</th>"; print "<th>Serial</th>"; print "<th>Total Size</th>"; print "<th>Used</th>"; print "<th>Free</th>"; print "<th>Use %</th>"; print "</tr>"; while($row = sqlsrv_fetch_array($stmt)) { print "<tr>"; $date = $row[0]; $result = $date->format('Ym-d'); print "<td>" . $result . "</td>"; for($i=1; $i<6; $i++) { print "<td>" . $row[$i] . "</td>"; } print "<td>" . $row[6]*100 . "%</td>"; print "</tr>"; } print "</table>"; print "<table id='datadomain' class='alltables'>"; print "<caption>DataDomain - Enterprise Vault</caption>"; print "<tr>"; print "<th>Date</th>"; print "<th>Type</th>"; print "<th>Location</th>"; print "<th>CTWDD</th>"; print "<th>CSC</th>"; print "<th>Total Size</th>"; print "<th>Used</th>"; print "<th>Free</th>"; print "<th>Use %</th>"; print "</tr>"; while($row = sqlsrv_fetch_array($stmt)) { print "<tr>"; $date = $row[0]; $result = $date->format('Ym-d'); print "<td>" . $result . "</td>"; for($i=1; $i<8; $i++) { print "<td>" . $row[$i] . "</td>"; } print "<td>" . $row[8]*100 . "%</td>"; print "</tr>"; } print "</table>"; 

该代码是在正确的PHP标签,我只是把它留下,因为有更多的表。