【问题标题】:Encoding UTF-8 when exporting HTML table to Excel将 HTML 表格导出到 Excel 时编码 UTF-8
【发布时间】:2014-11-02 00:17:29
【问题描述】:

我正在尝试使用 javascript 将 HTML 表格导出到 Excel。这是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> 

这是我的标题

<meta http-equiv="content-type" content="application/vnd.ms-excel;" charset="UTF-8">
<meta charset="UTF-8">

这是我的桌子

<table id="tblExport">
   <tr>
      <td>José</td>
      <td>María</td>
   </tr>
</table>

这是触发导出的按钮

<input type="button" onclick="tableToExcel('tblExport', 'W3C Example Table')" value="Export to Excel">

我无法正确导出 UTF-8 字符,例如 é 或 í。我试试这个Importing HTML table into OO Calc as UTF8 without converting to entities 但不起作用。我有 MS-Excel 2010 和 Win7 64 位。

如何正确导出 UTF-8 字符?

谢谢!

【问题讨论】:

    标签: javascript html excel encoding utf-8


    【解决方案1】:

    首先:您的标头格式错误。应该是:

    <meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">
    

    其次:它应该在您的模板中,因为它包含 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"><meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8"><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> 
    

    【讨论】:

    • 您能分享一下您找到的简单解决方案吗?谢了!
    • 感谢分享简单的解决方案:D
    • 那么简单的解决方案是什么?
    • 关于“解决方案更简单”的任何消息?? ^_^
    • 你的回答很好,你写的函数对我有帮助+1
    【解决方案2】:
    function exportData(report_id){
        var blob = new Blob([document.getElementById(report_id).innerHTML], {
            type: "text/plain;charset=utf-8;"
        });
        saveAs(blob, "Report.xls");
    }
    

    将表格数据作为纯文本保存为Excel,没有编码问题

    【讨论】:

    • 我很难做出这样的工作,Blob 上还有一个“编码”属性。但是,每个 Excel 程序都会以不同的方式解释“保存为 .xls 文件的 html”,所以谁知道呢。
    【解决方案3】:

    在 var uri 中使用以下代码:

    var uri = 'data:application/vnd.ms-excel;charset=UTF-8;base64,'
    

    Output

    【讨论】:

      【解决方案4】:

      我将再次引用上面指出的尊重。您需要在 head 标签内包含 meta 标签代码:

      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <meta charset="utf-8" />
      <!--This is what you should include-->
      <meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">
      <!---->
      <title>Ver Listado Pago</title>
      <link href="/Images/Decretos.ico" rel="shortcut icon" type="image/x-icon" />
      <meta name="viewport" content="width=device-width" />
      <link href="/Content/site.css" rel="stylesheet" />
      <link href="~/Content/booostrap/bootstrap.min.css" rel="stylesheet" />
      <link href="~/Content/booostrap/bootstrap-theme.css" rel="stylesheet" />
      <link href="~/Content/booostrap/bootstrap-theme.min.css" rel="stylesheet" />
      

      这对我有用。但是 IE 和 WIN10 因为 xls 扩展名在下载时有些冲突。 不过,特殊字符的问题已修正

      【讨论】:

        【解决方案5】:

        如果你想改变默认文件名according to @Axel Richter's answer,试试这个:

        var link = document.createElement('a');
        link.download = 'filename.xls';
        ...
        link.href = uri + base64(format(template, ctx));
        link.click();
        ...
        

        window.location.href 替换为 link.href

        【讨论】:

          【解决方案6】:

          您可以在内容字符串前面加上 UTF-8 BOM 定义:

          function ExportToExcel() {
              var BOM = "\uFEFF";
              var htmltabel = document.getElementById("tabella_finale");
              var html = htmltabel.outerHTML;
              window.open('data:application/vnd.ms-excel,'+ encodeURI(BOM + html));
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-06-11
            • 2016-09-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-09-22
            • 2018-05-01
            相关资源
            最近更新 更多