从TableToExcel JavaScript打开导出的表时发出警告

时间:2018-08-22 05:58:12

标签: javascript jquery html excel

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

1 个答案:

答案 0 :(得分:-1)

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

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