2012-12-02 17 views
56

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

114
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.

+1

Come formattare le intestazioni di datatable durante l'esportazione ?? –

+0

Sì. questo funziona bene. Ma quando il datatable contiene righe in lakhs. Questo non funziona. – thevan

+1

Forse dovresti aprire una domanda per quel caso specifico allora? – wegginho

10

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);       
} 
+0

Grazie per il frammento! Nessuna necessità di urlencode "Logs.xlsx" :) –

2

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(); 

     } 
+0

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 –