如何使用单个id将多个html表导出为ex​​cel

时间:2014-08-07 07:49:11

标签: javascript jquery html5 excel dom

我有两个html表,它们动态地使用不同的数据。无法将两者导出到Excel工作表我愿意导出单个表。

<div class="data" style="display: block;">

    <table id="testTable" border="1">
    <tbody>
    <table style="width:300px">
    <tr>
      <td>John</td>
      <td>Doe</td>      
      <td>80</td>
    </tr>
    <tbody>
    </table>

</div>
<div class="data" style="display: block;">

    <table id="testTable" border="1">
    <tbody>
    <table style="width:300px">
    <tr>
      <td>vsr</td>
      <td>sub</td>      
      <td>90</td>
    </tr>
    <tbody>
    </table>

</div>

像这样,我的表带有来自db

的不同数据集

我的js代码是

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((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.getElementById(resultarea)
    var ctx = {worksheet: name || 'Worksheet', table: resultarea.innerHTML}

    document.getElementById("dlink").href = uri + base64(format(template, ctx));
    document.getElementById("dlink").download = filename;
    document.getElementById("dlink").click();

  }
})()

无法同时导出到excel两个表数据。请帮帮我。

2 个答案:

答案 0 :(得分:0)

您应该通过classname引用,而不是通过id引用。对于您现在遇到的错误类型,建议不要对多个HTML元素使用相同的ID。

答案 1 :(得分:0)

done = False
while not done:
    print('=========================================')
    print('  Aria\'s Pet Store Point of Sale System')
    print('=========================================')
    print('[1] Add Products to Cart')
    print('[2] Remove Products to Cart')
    print('[3] View Cart')
    print('[4] Generate Reciept')
    print('[5] Logout')
    print('[6] Exit')
    print('=========================================')
    choice = input('Choice: ')

if choice == '1':
    pass
elif choice == '2':
    pass
elif choice == '3':
    pass
elif choice == '4':
    pass
elif choice == '5':
    pass
elif choice == '6':
    exit(0)
else:
    print('Invalid Input')