【问题标题】:Javascript to export html table to Excel将 html 表导出到 Excel 的 Javascript
【发布时间】:2013-06-13 02:33:06
【问题描述】:

当用户单击“导出”按钮时,我需要将页面中的 html 表导出到 Excel。现在,我在这里找到了适用于 Firefox 的堆栈溢出解决方案。

Export dynamic html table to excel in javascript in firefox browser

现在,它不处理像 ö,ü,ö 这样的特殊字符,这些字符在我们在这里使用的语言中很常见,所以我想问一下是否有人知道我如何才能成功地导出它们而不会出现问题?

这是我的代码:

 function tabletoExcel(table, name) {
    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]; }); };
        if (!table.nodeType) table = document.getElementById(table);
        var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML };
        window.location.href = uri + base64(format(template, ctx));

}

【问题讨论】:

  • 使用此方法,您只能导出页面中可见的任何内容。你同意这种方法吗?
  • 是的。这就是我想要做的。有时,有些名称带有特殊字符,并且无法正确导出。那么,我想知道我应该使用什么编码?
  • 它在 IE 中不起作用..
  • 如何扩展这个js,以便在excel中导出多个表格html?

标签: javascript excel


【解决方案1】:

如果你添加:

<meta http-equiv="content-type" content="text/plain; charset=UTF-8"/>

在文档的头部,它将按预期开始工作:

<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]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></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>

Updated Fiddle Here.

【讨论】:

  • @seroth 您收到此错误是因为您使用的是 IE caniuse.com/#search=btoa
  • 这在 IE 11 中对我不起作用。当您单击“导出”按钮时,它只会显示页面正在等待。
  • “download.xls 的文件格式和扩展名不匹配”这是一个问题,因为最终用户不想看到那里并点击是....
  • 如何用这个js导出多个表?
  • 非常感谢。你让我开心:)
【解决方案2】:

对于 UTF 8 转换和货币符号导出使用这个:

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 version="1.0" encoding="UTF-8" standalone="yes"?><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))
  }
})()

【讨论】:

    【解决方案3】:

    ShieldUI 的 export to excel 功能应该已经支持所有特殊字符。

    【讨论】:

      猜你喜欢
      • 2013-03-11
      • 2023-03-27
      • 2014-06-20
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 2011-09-27
      • 2015-08-05
      • 1970-01-01
      相关资源
      最近更新 更多