【问题标题】:Export Gridview to Excel with EPPLus使用 EPPLus 将 Gridview 导出到 Excel
【发布时间】:2018-12-29 03:38:36
【问题描述】:

我正在尝试使用 EPPlus 将数据从数据表导出到 Excel 文件,但似乎没有任何效果。 这是代码-

using (ExcelPackage xp = new ExcelPackage())
{
    ExcelWorksheet ws = xp.Workbook.Worksheets.Add(dt.TableName);

    int rowstart = 2;
    int colstart = 2;
    int rowend = rowstart;
    int colend = colstart + dt.Columns.Count;

    ws.Cells[rowstart, colstart, rowend, colend].Merge = true;
    ws.Cells[rowstart, colstart, rowend, colend].Value = dt.TableName;
    ws.Cells[rowstart, colstart, rowend, colend].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
    ws.Cells[rowstart, colstart, rowend, colend].Style.Font.Bold = true;
    ws.Cells[rowstart, colstart, rowend, colend].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    ws.Cells[rowstart, colstart, rowend, colend].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightGray);

    rowstart += 2;
    rowend = rowstart + dt.Rows.Count;
    ws.Cells[rowstart, colstart].LoadFromDataTable(dt, true);
    int i = 1;
    foreach (DataColumn dc in dt.Columns)
    {
        i++;
        if (dc.DataType == typeof(decimal))
        ws.Column(i).Style.Numberformat.Format = "#0.00";
    }
    ws.Cells[ws.Dimension.Address].AutoFitColumns();

    ws.Cells[rowstart, colstart, rowend, colend].Style.Border.Top.Style =
       ws.Cells[rowstart, colstart, rowend, colend].Style.Border.Bottom.Style =
       ws.Cells[rowstart, colstart, rowend, colend].Style.Border.Left.Style =
       ws.Cells[rowstart, colstart, rowend, colend].Style.Border.Right.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;


    Response.AddHeader("content-disposition", "attachment;filename=logs.xlsx");
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.BinaryWrite(xp.GetAsByteArray());
    Response.Close();
    HttpContext.Current.ApplicationInstance.CompleteRequest();
}

没有显示异常,代码运行流畅但没有创建文件。 我真的不知道如何理解这条线背后发生的事情,因为它什么也没显示。但我可以看到工作表中的单元格已填充。 我尝试了此站点和其他站点上提供的所有其他问题和解决方案。但似乎没有任何效果。所以请不要只提供其他问题和解决方案的链接。 任何其他类型的建议将不胜感激。

【问题讨论】:

  • 你调试代码了吗?设置断点?它是否达到了将字节写入响应的程度?
  • 正如你所说,...我在 response.BinaryWrite() 上设置了一个断点。当我更深入地检查时,响应在输出流上有一些异常,如读取超时、写入超时。如果此异常很重要,我该如何解决。
  • 这个异常会因为数据类型而发生吗?我有一个日期时间列。可以通过这个错误

标签: asp.net gridview epplus


【解决方案1】:

试试这个。有用。 Response.BinaryWrite 在下载文件时效果不佳。而且你还没有设置content-length

byte[] bin = xp.GetAsByteArray();

Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-length", bin.Length.ToString());
Response.AddHeader("content-disposition", "attachment;filename=logs.xlsx");
Response.OutputStream.Write(bin, 0, bin.Length);
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();

【讨论】:

  • 在尝试您的代码后.. 得到相同的异常,outputstream.length,outputstream.position 不支持异常。输出流.readtimeout,输出流.writetimeout。这些异常没有通过 catch 显示。
  • 是不是因为我有excel 2016?
  • 不,这与是无关的。因为上面的代码有效,所以一定是其他原因导致了问题。也许你正在做一些不在你发布的代码中的事情?
【解决方案2】:

可能 Response.End() 和“application/ms-excel”内容类型将解决您的问题。试试下面的代码:

byte[] data= xp.GetAsByteArray();
Response.ContentType = "application/ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=logs.xlsx; size=" + data.Length);    
Response.BinaryWrite(data);
Response.End();

另外,如果它不起作用,请尝试在之前添加以下代码:

Response.Clear();
Response.ClearHeaders();
Response.ClearContent();

【讨论】:

  • 我有你在另一个页面上建议的相同代码..这适用于每个可能的文件...相同的代码在此页面上不起作用...我想导出擅长..我不明白这个页面有什么问题......作为母版页,其他一切都与另一个页面相同
【解决方案3】:

我在另一个页面的帮助下完成了这项工作,该页面的工作只是获取文件名和路径并发送到响应。 如果有人像我一样卡在你做所有事情的地方,事情在其他页面上工作,但不是你想要的地方。所以你可以试试这个。在这种情况下,不会进行刷新或闪烁。用户根本不会觉得有其他页面被使用。

CardLogs.aspx.cs

string filename = "logs_" + DateTime.Now.ToString("MMddyyyy") + ".xlsx";
                FileInfo excelFile = new FileInfo(Server.MapPath("~/Uploads/" + filename));
                excel.SaveAs(excelFile);
                Session["fileName"] = excelFile.FullName;
                Response.Redirect("exportdata.aspx"); 

exportdata.aspx.cs 文件 -

 protected void Page_Load(object sender, EventArgs e)
    {
        string filePath = HttpContext.Current.Session["fileName"].ToString();
        if (!string.IsNullOrEmpty(filePath))
        {
            Response.ContentType = ContentType;
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
            Response.WriteFile(filePath);
            Response.Flush();
            File.Delete(filePath);                
        }

    }

【讨论】:

    猜你喜欢
    • 2019-10-28
    • 2012-11-20
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    相关资源
    最近更新 更多