Mit EPPlus, ich bin versucht zu exportieren einer gridview in einem Excel-Sheet
Excel sagt mir, dass es nicht lesbar Daten, und so, nachdem ich sage, zu versuchen, die Informationen wiederherzustellen, es zeigt die richtigen Daten. Wenn ich die Textdatei öffnen der xlsx, obwohl, ich bekommen alle die html für die ganze Seite, statt nur die gridview (was vermutlich nicht lesbare Inhalte von Excel ist reden).
Hier ist mein code:
public void ExcelDownload(object sender, EventArgs e)
{
DataSet _MailingListUsers = db.GetMailingList();
DataTable mailTable = _MailingListUsers.Tables[0];
DumpExcel(mailTable);
}
private void DumpExcel(DataTable tbl)
{
using (ExcelPackage pck = new ExcelPackage())
{
//Create the worksheet
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Mailing List");
//Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
ws.Cells["A2"].LoadFromDataTable(tbl, false);
//Header Titles
ws.Cells["A1"].Value = "Employee Name";
ws.Cells["B1"].Value = "Email Address";
ws.Cells["C1"].Value = "Phone";
ws.Cells["D1"].Value = "Business Unit";
ws.Cells["E1"].Value = "Site";
ws.Cells["A1"].AutoFitColumns();
//Format the header for column 1-3
using (ExcelRange rng = ws.Cells["A1:E1"])
{
rng.Style.Font.Bold = true;
//Set Pattern for the background to Solid
rng.Style.Fill.PatternType = ExcelFillStyle.Solid;
//Set color to dark blue
rng.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(79, 129, 189));
rng.Style.Font.Color.SetColor(System.Drawing.Color.White);
}
//Write it back to the client
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=MailingList.xlsx");
Response.BinaryWrite(pck.GetAsByteArray());
}
}
Irgendwelche Ideen, was könnte passiert sein? Jemand schlug vor, mit EPPlus nachdem ich die gleichen Probleme hatten, versucht zu senden, HTML Daten nach Excel, und er schickte die ganze Seite, statt nur die gridView.
Dank
InformationsquelleAutor shawleigh17 | 2012-03-05
Du musst angemeldet sein, um einen Kommentar abzugeben.
Mir fehlt der
Antwort.Clear()
und dieAntwort.Ende()
:Beste Lösung..........................................................!!! 😀
Ich möchte nur darauf hinweisen, die Zeile 4 sollte wie folgt sein: ws.Zellen["A2"].LoadFromDataTable(tbl, false);
Wäre es Ihnen möglich, bitte unterstützen Sie mich hier: stackoverflow.com/questions/27827277/...
InformationsquelleAutor Rango