Hi

creating excel using datatable

public static void ExportExcel(DataTable dtTable)
 {
 GridView grdEmp = new GridView();
 HttpContext.Current.Response.Clear();
 HttpContext.Current.Response.Buffer = true;
 HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=EmployeeExport.xls");
 HttpContext.Current.Response.Charset = "";
 HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
 StringWriter sw = new StringWriter();
 HtmlTextWriter hw = new HtmlTextWriter(sw);
 grdEmp.AllowPaging = false;
 grdEmp.DataSource = dtTable;
 grdEmp.DataBind();
 for (int i = 0; i < grdEmp.Rows.Count; i++)
 {
 GridViewRow row = grdEmp.Rows[i]; //Change Color back to white
 row.BackColor = System.Drawing.Color.White; //Apply text style to each Row row.Attributes.Add("class", "textmode"); //Apply style to Individual Cells of Alternating Row
 }
 grdEmp.RenderControl(hw); //style to format numbers to string //string style = @""; //HttpContext.Current.Response.Write(style); HttpContext.Current.Response.Output.Write(sw.ToString());
 HttpContext.Current.Response.Flush();
 HttpContext.Current.Response.End();
 }
Previous
Next Post »