I have a requirement for export data to excel using open Xml plug in. And I want to style excel head boarder colour. I tried different method to achieve my requirement. I would like to export my data table to excel.
My Code as follows
public ActionResult ExportToExcel()
{
var products = new System.Data.DataTable("teste");
products.Columns.Add("Name", typeof(string));
products.Columns.Add("Age", typeof(int));
products.Columns.Add("Gender", typeof(string));
products.Columns.Add("Salary", typeof(int));
products.Rows.Add("Basil", 34, "male", 40000);
products.Rows.Add("Varghese", 34, "male", 40000);
products.Rows.Add("George", 34, "male", 40000);
products.Rows.Add("John", 34, "male", 40000);
var grid = new GridView();
grid.DataSource = products;
grid.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
grid.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
return View("MyView");
}