C# 大型数据集下载到excel

C# 大型数据集下载到excel,c#,asp.net,export-to-excel,epplus,C#,Asp.net,Export To Excel,Epplus,我正在使用EPPlus下载大型excel文件。当我看到该文件时,它会将数据写入其中,但在下载时,它会显示超时过期错误! 我有下面的代码 dtExport.Rows.Add(drFooter); DataSet ds = new DataSet(); ds.Tables.Add(dtExport); try { FileInfo template = new FileInfo(HttpContext.Curren

我正在使用EPPlus下载大型excel文件。当我看到该文件时,它会将数据写入其中,但在下载时,它会显示超时过期错误! 我有下面的代码

     dtExport.Rows.Add(drFooter);
     DataSet ds = new DataSet();
     ds.Tables.Add(dtExport);
        try
        {
            FileInfo template = new FileInfo(HttpContext.Current.Server.MapPath(@"\ExcelPackageTemplate.xlsx"));
            FileInfo newfile = new FileInfo((filePath + filename));

            var rowCount = dtExport.Rows.Count;
            using (ExcelPackage pck = new ExcelPackage(newfile, template))
            {
                /**************Create Worksheet with datatable-- No Formatting************/
                ExcelWorkbook wb = pck.Workbook;
                ExcelWorksheet sheet = wb.Worksheets["Sheet1"];
                sheet.Cells["A1"].LoadFromDataTable(dtExport, true);

                string[] columnTypes = new string[dtExport.Columns.Count];

                for (int i = 0, j = 1; i <= dtExport.Columns.Count - 1; i++, j++)
                {
                    if (dtExport.Columns[i].ColumnName == "Date")
                    {
                        sheet.Column(j).Width = 12;
                    }
                    else
                    {
                        sheet.Column(j).AutoFit();
                    }
                }

                pck.Save();

                pck.Dispose();
              }
            }
          Response.TransmitFile((Server.MapPath("Excel/Rpt" + fileName)));

        }
        catch (Exception ex)
        {
            throw ex;
        }
dtExport.Rows.Add(drFooter);
数据集ds=新数据集();
ds.Tables.Add(dtExport);
尝试
{
FileInfo template=newfileinfo(HttpContext.Current.Server.MapPath(@“\ExcelPackageTemplate.xlsx”);
FileInfo newfile=newfileinfo((filePath+filename));
var rowCount=dtExport.Rows.Count;
使用(ExcelPackage pck=新的ExcelPackage(新文件、模板))
{
/**************使用datatable创建工作表--无格式************/
Excel工作簿wb=pck.工作簿;
ExcelWorksheet sheet=wb.工作表[“Sheet1”];
sheet.Cells[“A1”].LoadFromDataTable(dtExport,true);
string[]columnTypes=新字符串[dtExport.Columns.Count];

对于(int i=0,j=1;i调整executionTimeout可能会解决您在默认情况下遇到的超时问题,超时设置为110秒。您可以找到有关此问题的更多信息,以及

更新1:

我发现可能调用Response.Flush();after Response.TransmitFile可能会有所帮助,但我仍在试图找出原因。如果我找到一个好的理由,我会继续跟进

更新2:

根据MSDN文档,Response.TrasmitFile没有缓冲区,这意味着Response.Flush应该不会影响其处理

更新3:

我查阅了EPPlus的源代码,他们有一些使用ASP.NET处理下载的示例代码


我在updatepanel中更新了AsyncPostBackTimeout=“600”,它成功了!

我尝试的那一个返回了将近127269行,但它可以包含比这更大的行!我猜想您必须更改内容类型和配置以匹配它。
Response.BinaryWrite(pck.GetAsByteArray());
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;  filename=Sample3.xlsx");