Ich möchte eine Datentabelle mit EPPlus in eine Excel-Datei exportieren. Diese Datentabelle hat eine Eigenschaft vom Typ int, daher möchte ich dasselbe Format in der Excel-Datei.
Kennt jemand die Möglichkeit, eine solche DataTable nach Excel zu exportieren?
und wenn Sie in Browserantwort herunterladen möchten
Response.Clear(); Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode("Logs.xlsx", System.Text.Encoding.UTF8)); using (ExcelPackage pck = new ExcelPackage()) { ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Logs"); ws.Cells["A1"].LoadFromDataTable(dt, true); var ms = new System.IO.MemoryStream(); pck.SaveAs(ms); ms.WriteTo(Response.OutputStream); }
quelle
Zum Herunterladen von Excel-Tabellen im Browser verwenden Sie
HttpContext.Current.Response
stattResponse
sonst erhalten SieResponse is not available in this context.
Fehler. Hier ist mein Codepublic void ExporttoExcel(DataTable table, string filename) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=GridData.xlsx"); using (ExcelPackage pack = new ExcelPackage()) { ExcelWorksheet ws = pack.Workbook.Worksheets.Add(filename); ws.Cells["A1"].LoadFromDataTable(table, true); var ms = new System.IO.MemoryStream(); pack.SaveAs(ms); ms.WriteTo(HttpContext.Current.Response.OutputStream); } HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); }
quelle
Hier ist ein Ausschnitt zum Exportieren von DataSet nach Excel:
private static void DataSetToExcel(DataSet dataSet, string filePath) { using (ExcelPackage pck = new ExcelPackage()) { foreach (DataTable dataTable in dataSet.Tables) { ExcelWorksheet workSheet = pck.Workbook.Worksheets.Add(dataTable.TableName); workSheet.Cells["A1"].LoadFromDataTable(dataTable, true); } pck.SaveAs(new FileInfo(filePath)); } }
Und mit Aussagen:
using OfficeOpenXml; using System.Data; using System.IO;
quelle
Hier sind einige Zahlenformatierungen für Epplus-Zellen
//integer (not really needed unless you need to round numbers, Excel will use default cell properties) ws.Cells["A1:A25"].Style.Numberformat.Format = "0"; //integer without displaying the number 0 in the cell ws.Cells["A1:A25"].Style.Numberformat.Format = "#"; //number with 1 decimal place ws.Cells["A1:A25"].Style.Numberformat.Format = "0.0"; //number with 2 decimal places ws.Cells["A1:A25"].Style.Numberformat.Format = "0.00"; //number with 2 decimal places and thousand separator ws.Cells["A1:A25"].Style.Numberformat.Format = "#,##0.00"; //number with 2 decimal places and thousand separator and money symbol ws.Cells["A1:A25"].Style.Numberformat.Format = "€#,##0.00"; //percentage (1 = 100%, 0.01 = 1%) ws.Cells["A1:A25"].Style.Numberformat.Format = "0%"; //accounting number format ws.Cells["A1:A25"].Style.Numberformat.Format = "_-$* #,##0.00_-;-$* #,##0.00_-;_-$* \"-\"??_-;_-@_-";
quelle