【问题标题】:HTML Table - Excel Export Not Working Google Chrome and IEHTML 表格 - Excel 导出不起作用 Google Chrome 和 IE
【发布时间】:2019-07-01 22:14:26
【问题描述】:

我在 IE 和谷歌 chrome 浏览器上导出 HTML 表格时遇到问题,该表格有 2513 行表格数据。我在表单上读到 google chrome 有一个 URL 限制错误。

如果我从表格中删除一些内容并使其低于 2200 行,则导出到 excel 工作正常。

但是对于有 2600 行的当前表,导出到 excel 只会在谷歌浏览器上打开一个空白页

以下是我导出到 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 html excel html-table export-to-excel


【解决方案1】:

我创建了一个 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>

【讨论】:

  • 非常感谢@jordiburgos
  • 我必须下载这个程序并将其保存在我的浏览器路径下还是我应该将它下载到我的服务器 HTML 表格数据路径中
  • 它必须在服务器端。下载 lib 并使用 script 标签加载它。查看de文档了解更多详情
  • 非常感谢您的帮助我会测试它
  • 抱歉给您添麻烦了。我已经安装了库并尝试下载它会打开包含内容 {table} 的空白 excel 页面。我的服务器端没有安装 Javascript。有什么遗漏吗?我在 IBM HTTP 服务器上使用 AIX 7.1 操作系统
猜你喜欢
  • 2014-05-07
  • 2016-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多