问题
I am trying to export data from datatable to Excel file using EPPlus, but nothing seems to work. This is the code-
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();
}
no exception is shown, code runs smoothly but no file is created. I really dont know how to get what's happening behind this lines as its showing nothing. But I can see the cells in the sheet are filled. I tried every other question and solution available on this site and other sites also. But nothing seems to work. so please dont only provide link to other questions and solutions. Any other kind of advice will be appreciated.
回答1:
Try this. It works. Response.BinaryWrite
does not work very well when downloading a file. And you haven't set the 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();
回答2:
Probably Response.End() and "application/ms-excel" content type will solve your problem. Try the following code:
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();
Also if it's not working try to add the following code before:
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
回答3:
I have done this with help of another page who's job was just to take the file name and path and send to response. If anyone stucks like me where you are doing everything right, things working on every other page but not where you want. So you can try this. No refresh or blinking will be done in this case. User wont feel that there was other page used at all.
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 file -
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);
}
}
来源:https://stackoverflow.com/questions/53966492/export-gridview-to-excel-with-epplus