【问题标题】:Failing to load data into excel file when exporting HTML table to Excel将 HTML 表格导出到 Excel 时无法将数据加载到 Excel 文件中
【发布时间】:2015-04-10 05:27:26
【问题描述】:

我想将 HTML 表格导出到 excel 文件。问题是表内的数据没有导出。表头已成功导出,但数据未成功导出。我也有编码问题。某些特殊字符无法正确显示。有人知道问题出在哪里吗?

这是我的代码:

HTML 表格:

<table id"table2excel">
    <tbody>
    <tr>
        <th>First Name</th><th>Last name</th><th>Score</th>
    </tr>
    <tr>
        <td>John</td><td>Doe</td><td>23124</td>
    </tr>
    <tr>
        <td>Jane</td><td>Doe</td><td>35555</td>
    </tr>
    </tbody>
</table>

JavaScript

 <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>

【问题讨论】:

    标签: javascript jquery html export-to-excel


    【解决方案1】:

    您的 html 和脚本中有错误。

    <table id"table2excel">
    

    应该是

    <table id="table2excel">
    

    我创建了一个小提琴:

    http://jsfiddle.net/Sourabh_/5ups6z84/2/

    【讨论】:

    • 我喜欢你包括一个小提琴。
    • @Zee 嘿,这很好用,但是如果我想在单击按钮时获得同样的效果,如何修改它?
    猜你喜欢
    • 1970-01-01
    • 2014-04-08
    • 2014-10-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2015-08-05
    • 2017-07-14
    • 1970-01-01
    相关资源
    最近更新 更多