代码之家  ›  专栏  ›  技术社区  ›  SP1

将页面上的多个HTML表保存为csv

  •  0
  • SP1  · 技术社区  · 6 年前

    我有一个页面上有多个HTML表。我想使用js或jquery将所有表下载到一个csv或excel文件中。我搜索了它,找到了一些jquery插件,它们可以做到这一点。

    https://www.jqueryscript.net/table/Table-To-CSV-jQuery-csvExport.html 
    

    但问题是,它们只对页面上的一个表起作用。我希望将页面上的所有表保存在一个Excel工作表中。是否有人在将一个页面上的多个HTML表导出到一个Excel文件中,该文件也可在IE中使用?

    谢谢

    1 回复  |  直到 6 年前
        1
  •  1
  •   JoshG    6 年前

    看起来我们需要使用一个blob作为edge/ie(基于 this related answer )试一试。我在边缘测试了它,它似乎工作。

    注释 :您将无法单击下面代码段中的“导出”按钮,因为代码位于沙盒环境中。我给你做了一把小提琴来测试它: https://jsfiddle.net/ratk2pmz/

    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
        }
    
        var HeaderName = 'Download-ExcelFile';
        var ua = window.navigator.userAgent;
        var msieEdge = ua.indexOf("Edge");
        var msie = ua.indexOf("MSIE ");
    
        if (msieEdge > 0 || msie > 0) {
          if (window.navigator.msSaveBlob) {
            var dataContent = new Blob([base64(format(template, ctx))], {
              type: "application/csv;charset=utf-8;"
            });
    
            var fileName = "excel.xls";
            navigator.msSaveBlob(dataContent, fileName);
          }
          return;
        }
    
        window.open('data:application/vnd.ms-excel,' + encodeURIComponent(format(template, ctx)));
    
      }
    })()
    <table id="table">
      <tr>
        <th>Company</th>
        <th>Contact</th>
        <th>Country</th>
      </tr>
      <tr>
        <td>Alfreds Futterkiste</td>
        <td>Maria Anders</td>
        <td>Germany</td>
      </tr>
      <tr>
        <td>Centro comercial Moctezuma</td>
        <td>Francisco Chang</td>
        <td>Mexico</td>
      </tr>
      <tr>
        <td>Ernst Handel</td>
        <td>Roland Mendel</td>
        <td>Austria</td>
      </tr>
      <tr>
        <td>Island Trading</td>
        <td>Helen Bennett</td>
        <td>UK</td>
      </tr>
      <tr>
        <td>Laughing Bacchus Winecellars</td>
        <td>Yoshi Tannamuri</td>
        <td>Canada</td>
      </tr>
      <tr>
        <td>Magazzini Alimentari Riuniti</td>
        <td>Giovanni Rovelli</td>
        <td>Italy</td>
      </tr>
    </table>
    
    <button onclick="tableToExcel('table', 'SHEET1')" class="btn btn-primary">Export to Excel</button>