private void ExporttoExcel(DataTable table)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Write(@""-//W3C//DTD HTML 4.0 Transitional//EN"">");
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.xls");
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
//sets font
HttpContext.Current.Response.Write("");
HttpContext.Current.Response.Write("
");
//sets the table border, cell spacing, border color, font of the text, background, foreground, font height
HttpContext.Current.Response.Write(" +
"borderColor='#000000' cellSpacing='0' cellPadding='0' " +
"style='font-size:10.0pt; font-family:Calibri; background:white;'>
"
);
//am getting my grid's column headers
int columnscount = GridView_Result.Columns.Count;
for (int j = 0; j < columnscount; j++)
{ //write in new column
HttpContext.Current.Response.Write("" );
//Get column headers and make it as bold in excel columns
HttpContext.Current.Response.Write("");
HttpContext.Current.Response.Write(GridView_Result.Columns[j].HeaderText.ToString());
HttpContext.Current.Response.Write("
This blog is useful to all my friends who are working on the .Net Technology and wants to enhance their skills as well their problem solving ability.
Tuesday, October 2, 2012
Convert Datatable or Export Datatable to Excel file
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment