失败 - 下载 EPPlus.dll 制作的 excel 文件时出现网络错误

Failed - network error when downloading excel file made by EPPlus.dll

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

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

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

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

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

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

试试这个:

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();
}   

我有同样的问题,我用下面的代码解决了,注意文件名中的引号=\"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);

我不想使用 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; 
    }

我正在导出 html table 字符串变量 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();