【问题标题】:Failed - network error when downloading excel file made by EPPlus.dll失败 - 下载 EPPlus.dll 制作的 excel 文件时出现网络错误
【发布时间】:2016-09-21 13:17:49
【问题描述】:

我尝试从 asp.net c# web 表单应用程序下载由EPPlus.dll 制作的 excel 文件。但我失败了 - 网络错误。 需要注意的是,上面提到的错误只发生在chrome中,并且可以在其他浏览器中成功完成。

顺便说一下,这个错误不会发生在我的本地主机上,它只发生在主服务器上。

如果有人能解释这个问题的解决方案,那将非常有帮助。

http://www.irandnn.ir/blog/PostId/29/epplus

【问题讨论】:

  • 发布点击事件的代码或者这个文件实际发送到客户端的地方。

标签: c# asp.net excel epplus


【解决方案1】:

我在使用 Response.Clear() 和 Response.Close() 时遇到了同样的问题,并且不得不避免它们查看我的代码,如下所示。

Response.Buffer = true;
Response.ContentType = mimeType;
Response.AddHeader("Content-Disposition", "attachment; filename=" + nameOfFile);
Response.BinaryWrite(bytes);
Response.End();

【讨论】:

  • 奇怪的是,它一直在我的网站上清晰而关闭,然后突然我开始收到这个错误。使用 End 似乎已修复它,谢谢。
【解决方案2】:

试试这个:

using (ExcelPackage p = new ExcelPackage())
{
    //Code to fill Excel file with data.


    Byte[] bin = p.GetAsByteArray();

    Response.ClearHeaders();
    Response.ClearContent();
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", Nombre_Del_Libro + ".xlsx"));
    Response.BinaryWrite(bin);
    Response.Flush();
    Response.End();
}   

【讨论】:

  • 当我将扩展名设置为 ".xlsx" 时,它不会在开发环境的 Excel(2016) 中打开。当我将其更改为“.xlx”时,它可以工作。可能是什么原因?
【解决方案3】:

我不喜欢使用 response.End() 因为会抛出异常

    protected void DownloadFile(FileInfo downloadFile, string downloadFilename, string downloadContentType)
    {
        Byte[] bin = File.ReadAllBytes(downloadFile.FullName); 

        Response.ClearHeaders();
        Response.ClearContent();
        Response.ContentType = downloadContentType;
        Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", downloadFilename ));
        Response.BinaryWrite(bin);
        Response.Flush();
        Response.SuppressContent = true; 
    }

【讨论】:

    【解决方案4】:

    我遇到了同样的问题,我用下面的代码解决了,注意 filename=\"Name.xlsx\" 中的引号:

    Response.Buffer = true;
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.AppendHeader("content-disposition", "attachment; filename=\"Name.xlsx\"");
    //Writeout the Content  
    Response.BinaryWrite(bytes);
    

    【讨论】:

      【解决方案5】:

      我正在导出字符串变量 HTML 中的 html 表格代码

      遵循为我工作的代码

      byte[] myFile = Encoding.ASCII.GetBytes(HTML);
              Response.Clear();
              Response.Buffer = true;
              Response.AddHeader("Content-Type", "application/excel");
              Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0};", "BAGrowthReport.xls"));
              Response.AddHeader("content-length", myFile.Length.ToString()); //Here is the additional header which is required for Chrome.
              Response.BinaryWrite(myFile);
              Response.Flush();
              Response.Close();
      

      【讨论】:

        猜你喜欢
        • 2017-07-07
        • 2021-09-17
        • 2021-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多