M E D Y A T Ö R

MVC Kayıtları Excele Aktarma

09

MAYIS

2017

4275
View
Controller

public void GridExportToExcel()
{
    string dosyaAdi = "ornek_dosya_adi";
    var table = // Buraya veritabanınından gelen herhangi bir dataSource gelebilir.( DataTable, DataSet, kendi oluşturduğunuz, herhangi bir ICollection tipinde entitiy model)
    var grid = new GridView();
    grid.DataSource = table;
    grid.DataBind();
 
    Response.ClearContent();
    Response.AddHeader("content-disposition", "attachment; filename=" + dosyaAdi + ".xls");
 
    Response.ContentType = "application/excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
 
    grid.RenderControl(htw);
 
    Response.Write(sw.ToString());
    Response.End();
}


Script


Html