【问题标题】:Excel download in Internet Explorer without Dialog Box Prompt在没有对话框提示的 Internet Explorer 中下载 Excel
【发布时间】:2020-08-29 07:25:36
【问题描述】:

我正在尝试使用 javascript 实现下载 excel 功能


function fnExcelReport() {
    var tab_text = "<table border='2px'><tr bgcolor='#87AFC6'>";
    var textRange;
    var j = 0;
    tab = document.getElementById('table'); // id of table

    for (j = 0; j < tab.rows.length; j++) {
        tab_text = tab_text + tab.rows[j].innerHTML + "</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, ""); // reomves input params

    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");
    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer
    {
        txtArea1.document.open("txt/html", "replace");
        txtArea1.document.write(tab_text);
        txtArea1.document.close();
        txtArea1.focus();
        sa = txtArea1.document.execCommand("SaveAs", true, "Say Thanks to Sumit.xls");
    } else //other browser not tested on IE 11
        sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));

    return (sa);
}

它在 chrome 中运行良好。但在 Internet Explorer 中,会出现一个“另存为”对话框。有没有办法不用打开对话框就可以下载文件

【问题讨论】:

  • 为什么要标记 Excel?这与 Excel 无关。

标签: javascript google-chrome jsp internet-explorer


【解决方案1】:

IE 浏览器显示的另存为对话框,您无法通过代码对其进行控制。

在旧版本的 IE 浏览器中,有一个禁用下载提示的选项。

在最新版本的 IE 浏览器中,出于安全考虑,此选项已被删除。

所以现在没有办法禁用这个对话框。

您也可以参考此线程以查看有关此主题的讨论。

How can I disable the "open or save" prompt when I try to open a file with IE 10

【讨论】:

    猜你喜欢
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 2014-11-07
    • 2012-05-29
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多