【发布时间】: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() 上设置了一个断点。当我更深入地检查时,响应在输出流上有一些异常,如读取超时、写入超时。如果此异常很重要,我该如何解决。
-
这个异常会因为数据类型而发生吗?我有一个日期时间列。可以通过这个错误