下载EPPlus.dll生成的excel文件时出现网络错误
本文关键字:网络 错误 文件 excel dll EPPlus 下载 | 更新日期: 2023-09-27 18:08:10
我尝试从asp.net c# web表单应用程序下载由EPPlus.dll
制作的excel文件。但我得到失败-网络错误。值得注意的是,上面提到的错误只发生在chrome浏览器中,在其他浏览器中也可以成功完成。
当我使用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();
}
我不喜欢使用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;
}
我遇到了同样的问题,我用下面的代码解决了,注意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);
我正在导出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();