【问题标题】:Cannot export to Excel in IE 11无法在 IE 11 中导出到 Excel
【发布时间】:2015-03-26 01:57:14
【问题描述】:

我有一个将数据从 gridview 导出到 Excel 的 Web 应用程序。它在 Chrome 中运行良好,但在 IE 11 和 Firefox 中无法运行。当我用谷歌搜索时,我发现了很多与此相关的问题,但这个错误似乎是独一无二的。

protected void ExportToExcel(List<MapAmericas.Model.Project> Projects)
{
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "application/vnd.ms-excel";
    Response.ContentEncoding = System.Text.Encoding.Unicode;
    Response.AddHeader("content-disposition", "attachment;filename=MyFile" + DateTime.Now.ToString() + ".xls");
    Response.Charset = "";
    Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
    this.EnableViewState = false;

    System.IO.StringWriter sw = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

    GridView gv = new GridView();
    gv.AutoGenerateColumns = false;
    gv.RowDataBound += new GridViewRowEventHandler(dataExportExcel_ItemDataBound);

    gv.DataSource = Projects;
    gv.DataBind();

    gv.RenderControl(htw);
    Response.Write(AddExcelStyling()); //this contains the opening html elements
    StringBuilder sbResponseString = new StringBuilder();
    sbResponseString.Append(sw + "</body></html>");
    Response.Write(sbResponseString.ToString());
    Response.Flush();
    Response.Close();
    Response.End();
}

...我遇到的问题与无法访问 Internet 临时文件有关。我收到一个弹出窗口“加载期间的问题”和大约十几个条目:“缺少文件...临时 Internet 文件\内容\”,然后我收到一条消息“无法读取文件”。 我有一张图片来表示错误,但我无法上传图片。 当我单击其中一个链接时,它会加载我的网页而没有任何样式,并且我没有获得 excel 文件。 有谁知道会是什么问题?

【问题讨论】:

  • 尝试替换响应。使用 HttpContext.Current.Resoponse。 ?
  • 也尝试注释掉 Response.Flush() 看看是否也有效。
  • 您不是在生成实际的 Excel 文件,而是在生成伪装成 Excel 文件的 HTML 文件。这有许多与之相关的问题。我在my blog 上描述了如何生成真正的 Excel 文件。
  • 尝试了 HttpContext,Current 并且没有用...还尝试注释掉 Response.Flush 并且由于某种原因,每次尝试导出到 Excel 时都会提示我登录。我会看看梅森的做法。

标签: asp.net internet-explorer export-to-excel internet-explorer-11


【解决方案1】:

我明白了!我在现有代码中添加了 2 行代码,它可以工作。查看标有**的行:

HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Buffer = true;
        //HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
        HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=Panorama_Project-" + DateTime.Now.ToString() + ".xls");

        HttpContext.Current.Response.Charset = "";
        HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
        this.EnableViewState = false;

        System.IO.StringWriter sw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

        GridView gv = new GridView();
        gv.AutoGenerateColumns = false;

        gv.RowDataBound += new GridViewRowEventHandler(dataExportExcel_ItemDataBound);

        gv.DataSource = Projects;
        gv.DataBind();

        gv.RenderControl(htw);
        HttpContext.Current.Response.Write(AddExcelStyling());
        StringBuilder sbResponseString = new StringBuilder();
        sbResponseString.Append(sw + "</body></html>");
        **HttpContext.Current.Response.AddHeader("Content-Length", sbResponseString.Length.ToString());**
        HttpContext.Current.Response.Write(sbResponseString.ToString());
        //HttpContext.Current.Response.Flush();
        //HttpContext.Current.Response.Close();
        //HttpContext.Current.Response.End();
        **System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();**

似乎它试图显示包括我的页面和所有链接资源的整个响应对象。 Content-Length 并用 System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest() 替换 Reponse.End 和 Response.Flush 似乎可以解决问题。

【讨论】:

    【解决方案2】:

    这是我使用的“导出到 Excel”函数(用于数据表)的简化版本,适用于所有浏览器。看看这是否有帮助。该代码与您的代码非常相似:

    public void ExportDataToExcel(String FileName, DataTable dtData)
    {
        // get gridview out of datatable
        GridView gv = new GridView();
        gv.AllowPaging = false;
        gv.DataSource = dtData;
        gv.DataBind();
    
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.AddHeader("content-disposition",
            "attachment;filename=" + FileName + ".xls");
        HttpContext.Current.Response.Charset = "";
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
    
        for (int i = 0; i < gv.Rows.Count; i++)
        {
            // Apply text style to each Row
            gv.Rows[i].Attributes.Add("class", "textmode");
        }
        gv.RenderControl(hw);
    
        HttpContext.Current.Response.Output.Write(sw.ToString());
    
        // commenting this out to resolve this error
        // System.Web.HttpException: The remote host closed the connection.     
        // The error code is 0x800703E3.
        // HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();
    }
    

    【讨论】:

    • @RobVenable 您的代码是否收到任何特定的错误消息?
    • 它有时会工作,但我经常会收到“无法打开文件”,然后弹出“无法打开文件”,有时我的 excel 文件打开并且是空的,有时我弹出一个包含所有缺少的临时文件的链接的弹出窗口,当我单击其中一个链接时,我的网页在没有任何关联样式的 excel 文件中打开。我很难过!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多