从不在IE中工作的javascript导出到Excel?

从不在IE中工作的javascript导出到Excel?,javascript,jquery,internet-explorer,Javascript,Jquery,Internet Explorer,在我的asp.NETMVC4项目中,我使用Excel导出功能,该功能使用Javascript从客户端将HTML表导出到Excel文件 导出功能在Chrome和Firefox中运行良好,但在IE(任何浏览器)中都不起作用。在IE中,它只是打开了一个新窗口,什么也没有发生 下面给出了我的javascript代码 function Export(htmltable,filename) { var excelFile = "<html xmlns:o='urn:schema

在我的asp.NETMVC4项目中,我使用Excel导出功能,该功能使用Javascript从客户端将HTML表导出到Excel文件

导出功能在
Chrome
Firefox
中运行良好,但在
IE
(任何浏览器)中都不起作用。在IE中,它只是打开了一个新窗口,什么也没有发生

下面给出了我的javascript代码

function Export(htmltable,filename) {
            var excelFile = "<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'>";
            excelFile += "<head>";
            excelFile += "<!--[if gte mso 9]>";
            excelFile += "<xml>";
            excelFile += "<x:ExcelWorkbook>";
            excelFile += "<x:ExcelWorksheets>";
            excelFile += "<x:ExcelWorksheet>";
            excelFile += "<x:Name>";
            excelFile += "{worksheet}";
            excelFile += "</x:Name>";
            excelFile += "<x:WorksheetOptions>";
            excelFile += "<x:DisplayGridlines/>";
            excelFile += "</x:WorksheetOptions>";
            excelFile += "</x:ExcelWorksheet>";
            excelFile += "</x:ExcelWorksheets>";
            excelFile += "</x:ExcelWorkbook>";
            excelFile += "</xml>";
            excelFile += "<![endif]-->";
            excelFile += "</head>";
            excelFile += "<body>";
            excelFile += htmltable.replace(/"/g, '\'');
            excelFile += "</body>";
            excelFile += "</html>";

            var base64data = "base64," + $.base64.encode(excelFile);
            window.open('data:application/vnd.ms-excel;'+ base64data);
 }
但它的命名仍然像“download.xls”

如何解决这些问题

我也发现了这些说法,

如果您将Internet Explorer作为浏览器,则必须寻找不同的方法,因为当前的方法不起作用。从MSDN库中,数据协议主题显示:

Data URIs are supported only for the following elements and/or attributes.

object (images only)
img
input type=image
link
CSS declarations that accept a URL, such as background, backgroundImage, 
and so on.

Data URIs can be nested.

For security reasons, data URIs are restricted to downloaded resources. Data URIs cannot be used for navigation, for scripting, or to populate frame or iframe elements.
有什么办法可以克服这个问题


非常感谢您的帮助。

出于安全原因,如果IE不再支持以那种形式生成文件,请更新版本

我建议的解决方案(并经过IE测试)是在MVC项目中添加一个新的通用处理程序。对于这个演示,我将我的ashx处理程序命名为“Downloader.ashx”。将此代码添加到通用处理程序:

    public void ProcessRequest(HttpContext context)
        {
            try
            {
                if (context.Request.QueryString.AllKeys.Contains("fileName"))
                {
                    StreamExcelorWordFile();
                }
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex.Message.ToString());
            }
        }

        public void StreamExcelorWordFile()
        {
            string tableName = HttpContext.Current.Request.QueryString["fileName"].ToString();
            string extensions = HttpContext.Current.Request.QueryString["extension"].ToString();

            //HttpContext.Current.Response.ContentType = "application/force-download";

            string appType = "force-download";
            if (extensions == "xls")
            {
                appType = "vnd.ms-excel";
            }
            else if (extensions == "doc")
            {
                appType = "vnd.ms-word";
            }

            HttpContext.Current.Response.ContentType = "application/" + appType;
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + tableName + "." + extensions);

            HttpContext.Current.Response.Write(HttpContext.Current.Request.Form["exportdata"].ToString().Replace("⌐", "<").Replace("¬", ">"));
        }
public void ProcessRequest(HttpContext上下文)
{
尝试
{
if(context.Request.QueryString.AllKeys.Contains(“文件名”))
{
StreamXcelorOrdFile();
}
}
捕获(例外情况除外)
{
HttpContext.Current.Response.Write(ex.Message.ToString());
}
}
public void StreamExcelorOrdFile()文件
{
string tableName=HttpContext.Current.Request.QueryString[“fileName”].ToString();
字符串扩展名=HttpContext.Current.Request.QueryString[“扩展名”].ToString();
//HttpContext.Current.Response.ContentType=“应用程序/强制下载”;
字符串appType=“强制下载”;
如果(扩展==“xls”)
{
appType=“vnd.ms excel”;
}
else if(扩展=“单据”)
{
appType=“vnd.ms word”;
}
HttpContext.Current.Response.ContentType=“application/”+appType;
HttpContext.Current.Response.AddHeader(“内容处置”、“附件;文件名=“+tableName+””+扩展名);
HttpContext.Current.Response.Write(HttpContext.Current.Request.Form[“exportdata”].ToString().Replace(“⌐", ""));
}
然后,将Javascript更改为类似以下内容:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>Excel or Word Downloader</title>

     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    <script type="text/javascript">
        function convertToXLS() {
            var rawHTML = '<table>' + $('#tblTest').html() + '</table>';

            Export(rawHTML, 'myExcelFile', 'xls');
        }

        function Export(htmltable, filename, extension) {
            var JsonHndlrx = "Downloader.ashx";

            htmltable = htmltable.replace(/</g, '⌐').replace(/>/g, '¬');

            $("body").append('<form id="exportform" action="' + JsonHndlrx + '?fileName=' + filename + '&extension=' + extension + '" method="post" target="_blank"><input type="hidden" id="exportdata" name="exportdata" /></form>');
            $("#exportdata").val(htmltable);
            $("#exportform").submit().remove();
            return true;
        }
    </script>
</head>
<body>
    <div>
        <table id="tblTest">
            <tr>
                <th>
                    Column1
                </th>
                <th>
                    Column2
                </th>
                <th>
                    Column3
                </th>
            </tr>
            <tr>
                <td>
                    locationA-1
                </td>
                <td>
                    locationA-2
                </td>
                <td>
                    locationA-3
                </td>
            </tr>
            <tr>
                <td>
                    locationB-1
                </td>
                <td>
                    locationB-2
                </td>
                <td>
                    locationB-3
                </td>
            </tr>
            <tr>
                <td>
                    locationC-1
                </td>
                <td>
                    locationC-2
                </td>
                <td>
                    locationC-3
                </td>
            </tr>
        </table>
    </div>
    <div>
        <input id="btnToExcelDownload" type="button" value="Convert Table To XLS" onclick="convertToXLS(); return false;" />
    </div>
</body>
</html>

Excel或Word下载程序
函数convertToXLS(){
var rawHTML='+$('#tblTest').html()+';
导出(原始HTML、“myExcelFile”、“xls”);
}
函数导出(htmltable、文件名、扩展名){
var JsonHndlrx=“Downloader.ashx”;
htmltable=htmltable.replace(//g,,,');
$(“正文”)。附加(“”);
$(“#导出数据”).val(htmltable);
$(“#导出表单”).submit().remove();
返回true;
}
专栏1
专栏2
第3栏
位置A-1
位置A-2
位置A-3
位置B-1
位置B-2
位置B-3
位置C-1
位置C-2
位置C-3
Wolla!它应该可以在包括IE在内的所有主要浏览器上运行

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>Excel or Word Downloader</title>

     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    <script type="text/javascript">
        function convertToXLS() {
            var rawHTML = '<table>' + $('#tblTest').html() + '</table>';

            Export(rawHTML, 'myExcelFile', 'xls');
        }

        function Export(htmltable, filename, extension) {
            var JsonHndlrx = "Downloader.ashx";

            htmltable = htmltable.replace(/</g, '⌐').replace(/>/g, '¬');

            $("body").append('<form id="exportform" action="' + JsonHndlrx + '?fileName=' + filename + '&extension=' + extension + '" method="post" target="_blank"><input type="hidden" id="exportdata" name="exportdata" /></form>');
            $("#exportdata").val(htmltable);
            $("#exportform").submit().remove();
            return true;
        }
    </script>
</head>
<body>
    <div>
        <table id="tblTest">
            <tr>
                <th>
                    Column1
                </th>
                <th>
                    Column2
                </th>
                <th>
                    Column3
                </th>
            </tr>
            <tr>
                <td>
                    locationA-1
                </td>
                <td>
                    locationA-2
                </td>
                <td>
                    locationA-3
                </td>
            </tr>
            <tr>
                <td>
                    locationB-1
                </td>
                <td>
                    locationB-2
                </td>
                <td>
                    locationB-3
                </td>
            </tr>
            <tr>
                <td>
                    locationC-1
                </td>
                <td>
                    locationC-2
                </td>
                <td>
                    locationC-3
                </td>
            </tr>
        </table>
    </div>
    <div>
        <input id="btnToExcelDownload" type="button" value="Convert Table To XLS" onclick="convertToXLS(); return false;" />
    </div>
</body>
</html>