Вопрос

I am using EPPlus to download large excel file. when I see the file it writes the data into it but while downloading it says timeout expired error! I do have the following piece of code.

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

How can I achieve this?

Это было полезно?

Решение 2

I updated the AsyncPostBackTimeout="600" in updatepanel and it worked!

Другие советы

Adjusting the executionTimeout might address the timeout issue that you are running into by default the timeout is set to 110 seconds. You can find more information regarding this issue here and here.

Update 1:

I'm finding indications that possibly calling Response.Flush(); after Response.TransmitFile might help but I'm still trying to sort out the reasoning for this. I will follow up if I find a good reason.

Update 2:

According to the MSDN documentation Response.TrasmitFile does not buffer meaning that Response.Flush should have no effect on its processing.

Update 3:

I dug trough the source code for EPPlus and they have some sample code for handling downloads with ASP.NET here

Response.BinaryWrite(pck.GetAsByteArray());
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;  filename=Sample3.xlsx");
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top