HTML表格-Excel导出无法正常工作Google Chrome和IE

时间:2019-02-07 17:52:27

标签: javascript html excel html-table export-to-excel

在IE和Google chrome浏览器上导出HTML表时出现问题,该表有2513行表数据。我在Google chrome表格上阅读了关于URL限制的错误。

如果我从表格中删除了一些内容并使其低于2200行,则可以导出到excel。

但是对于当前有2600行的表,导出到excel只会在Google chrome上打开空白页

以下是我对Excel函数和代码的导出。您能告诉我IE和Chrome浏览器的修复/解决方法

服务器正在运行IBM HTTP APP服务器

<br>
<head>

        <title> Server Data</title>

</head>




<br>
<h1 align=center style=color:#DC143C> Servers data</h1>
<br>
<br>
<br>

<input type="button" value="Export to Excel" style="font-size:12pt;color:white;background-color:green;border:5px solid #336600;padding:3px;float: left;" onclick="exportToExcel('exTable')" />
<br>
<iframe id="txtArea1" style="display:none"></iframe>
<br>
<br>


<script>
        function exportToExcel(tableID){
            var tab_text="<table border='2px'><tr bgcolor='#87AFC6' style='height: 75px; text-align: center; width: 250px'>";
            var textRange; var j=0;
            tab = document.getElementById(tableID); // id of table

        for(j = 0 ; j < tab.rows.length ; j++)
        {

            tab_text=tab_text;
            tab_text=tab_text+tab.rows[j].innerHTML.toUpperCase()+"</tr>";
            //tab_text=tab_text+"</tr>";
        }

            tab_text= tab_text+"</table>";
            tab_text= tab_text.replace(/<A[^>]*>|<\/A>/g, ""); //remove if u want links in your table
            tab_text= tab_text.replace(/<img[^>]*>/gi,""); //remove if u want images in your table
            tab_text= tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); //remove input params

            var ua = window.navigator.userAgent;
            var msie = ua.indexOf("MSIE ");
            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 (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
        {
                txtArea1.document.open("txt/html","replace");
                txtArea1.document.write( '\r\n' + tab_text);
                txtArea1.document.close();
                txtArea1.focus();
                sa=txtArea1.document.execCommand("SaveAs",true,"download.xls");
        }
        else {
            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>'
            var ctx = { worksheet: name || 'Worksheet', table:tab_text }
            sa = window.open('data:application/vnd.ms-excel;base64,' +base64(format(template, ctx)));
        }

            return (sa);
    }
</script>

Chrome浏览器会在单击时打开一个空白页以导出为Excel

1 个答案:

答案 0 :(得分:0)

我创建了一个JavaScript库来处理在客户端导出到Excel的问题:https://github.com/jmaister/excellentexport/

简单用法:

function setPhoneLink() {
    if (screen.width > 640) {
        $("a[href^='tel:']").each(function(){
            $(this).replaceWith($(this).text());
        });
    }
} 

对于XLSX和不止一张纸:

<a download="somedata.xls" href="#" onclick="return ExcellentExport.excel(this, 'datatable', 'Sheet Name Here');">Export to Excel</a>