Voglio esportare la tabella di dati in file Excel con EPPlus che la tabella di dati ha proprietà con tipo int, quindi voglio nel file excel, lo stesso formato sarà. Qualcuno sa come esportare un DataTable in Excel con questi?Export DataTable per eccellere con EPPlus
risposta
using (ExcelPackage pck = new ExcelPackage(newFile))
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Accounts");
ws.Cells["A1"].LoadFromDataTable(dataTable, true);
pck.Save();
}
Questo dovrebbe fare il trucco per voi. Se i tuoi campi sono definiti come int EPPlus cast correttamente le colonne in un numero o float.
e se volete scaricare in risposta del browser
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);
}
Grazie per il frammento! Nessuna necessità di urlencode "Logs.xlsx" :) –
Per scaricare ExcelSheet in uso del browser HttpContext.Current.Response
invece di Response
altrimenti otterrete Response is not available in this context.
error.Here è il mio codice
public 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();
}
Per chi ottiene "System.Web.HttpContextBase" non contiene una definizione per "Corrente": Per ottenere un riferimento a HttpContext.Current è necessario sostituire HttpContext.Current con System.Web.HttpContext.Current http://stackoverflow.com/questions/19431820/system-web-httpcontextbase-does-not-contain-a-definition-for-current-mvc-4-wi –
- 1. primeNG DataTable - eccellere come filtro
- 2. csv per eccellere conversione
- 3. export delphi stringgrid to excel
- 4. Come esportare più tabelle html per eccellere?
- 5. angularjs DataTable
- 6. Tabella HTML da eccellere - PHP
- 7. Esiste un modo per forzare il cmdlet PowerShell -Export-CSV per mantenere un ordine di colonne specifico?
- 8. gridview data export to excel in asp.net
- 9. Emissione grande set di dati EPPlus con memoria esaurita Eccezione
- 10. Datatable group per con linq in vb.net
- 11. Ordinamento iniziale per PrimeFaces datatable con multisort
- 12. Condizione di formato per espressione EPPlus
- 13. Forza EPPLUS a leggere come testo
- 14. Comando equivalente Perforce per svn export
- 15. DataTable con colonne dinamiche
- 16. errore Namespace OfficeOpenXML EPPlus
- 17. PostgreSQL: export CSV con linebreaks sfuggiti
- 18. EPPlus salvataggi multipli
- 19. Formattazione condizionale per espressione utilizzando EPPlus
- 20. Entity classe per Datatable
- 21. gpg --export-secret-keys
- 22. Come esportare la tabella html per eccellere usando PHPExcel?
- 23. dataview Ordinato per DataTable
- 24. Converti JSON per DataTable
- 25. org-mode html export
- 26. eccellere testo estratto vba- tra 2 caratteri
- 27. parte epplus esiste già
- 28. Errori su EPPlus
- 29. Automated Export Failed
- 30. MongoDB import/export index
Come formattare le intestazioni di datatable durante l'esportazione ?? –
Sì. questo funziona bene. Ma quando il datatable contiene righe in lakhs. Questo non funziona. – thevan
Forse dovresti aprire una domanda per quel caso specifico allora? – wegginho