【问题标题】:Warning when opening exported table from TableToExcel javascript从 Table To Excel javascript 打开导出表时出现警告
【发布时间】:2018-08-22 14:06:34
【问题描述】:

从 TableToExcel javascript 打开导出表时出现警告。

您尝试打开的文件“MySheet.xls”位于不同的 格式比文件扩展名指定的格式。验证文件是 在打开文件之前未损坏并且来自受信任的来源。做 你想现在打开文件吗?

这是我的 javascript 代码:

 <script type="text/javascript">
    var tableToExcel = (function () {
        var ua = window.navigator.userAgent;
        var msie = ua.indexOf("MSIE");

        if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
        {
            return function (table, name) {
                if (!table.nodeType) table = document.getElementById(table).outerHTML
                txtArea1.document.open("txt/html", "replace");
                txtArea1.document.write(table);
                txtArea1.document.close();
                txtArea1.focus();
                sa = txtArea1.document.execCommand("SaveAs", true, "MySheet.xls");
                return sa;
            }
        }
        else {

            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) {
                var a = document.createElement('a');
                document.body.appendChild(a);
                //getting data from our div that contains the HTML table
                var ctx = { worksheet: 'Worksheet', table: document.getElementById('report_table').innerHTML }
                //window.location.href = uri + base64(format(template, ctx))
                a.href = uri + base64(format(template, ctx));
                //setting the file name
                a.download = 'MySheet' + '.xls';
                //triggering the function
                a.click();
                //just in case, prevent default behaviour
            }
        }

    })()
</script>

【问题讨论】:

  • support.microsoft.com/en-us/help/948615/… 警告是“扩展强化”的结果 - 基本上文件内容格式与基于扩展的预期不匹配。这是意料之中的,因为 HTML 不是 Excel 文件格式。
  • @TimWilliams 是的,我知道,但我不知道如何将 HTML 格式更改为 Excel 格式。你能帮忙吗?
  • @TimWilliams 该链接建议将表数据发送到服务器,但我想这样做而不发送到服务器。

标签: javascript jquery html excel


【解决方案1】:

使用 xls 的扩展名 xlsx ,请尝试让我知道。我们确实解决了这样的错误。

sa = txtArea1.document.execCommand("SaveAs", true, "MySheet.xlsx");

【讨论】:

  • 我试过了,但打不开。得到这个:excel 无法打开 MySheet .xlsx,因为文件格式或文件扩展名无效。
  • 谢谢..我会找到另一个解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-29
  • 1970-01-01
  • 2013-12-14
  • 2022-12-28
  • 1970-01-01
  • 2014-11-30
  • 1970-01-01
相关资源
最近更新 更多