簡體   English   中英

HTML表格-Excel導出無法正常工作Google Chrome和IE

[英]HTML Table - Excel Export Not Working Google Chrome and IE

我在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

我創建了一個JavaScript庫來處理在客戶端導出到Excel的問題: https : //github.com/jmaister/excellentexport/

簡單用法:

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

對於XLSX和不止一張紙:

<a download="somedata.xlsx" 
    href="#" 
    onclick="return ExcellentExport.convert({ anchor: this, filename: 'data_123.array', format: 'xlsx'},[{name: 'Sheet Name Here 1', from: {table: 'datatable'}}]);">
Export to CSV</a>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM