Export to Excell Helper

public static void GridExportToExcel(string fileName, object dataTable)
{
    var grid = new GridView();
    grid.DataSource = dataTable;
    grid.DataBind();

    HttpContext.Current.Response.ClearContent();
    HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}-{1}.xls", fileName, DateTime.Now.ToShortDateString().Replace(".", "")));
    HttpContext.Current.Response.ContentType = "application/ms-excel";
    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
    HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());

    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    grid.RenderControl(htw);

    HttpContext.Current.Response.Write(sw.ToString());
    HttpContext.Current.Response.End();
}

Kullanımı işe ağaıdaki gibidir.

UsefulHelpers.GridExportToExcel("Bankalar", table);