The following steps are required to export gridview to microsoft excel
Step1: Override the method "VerifyRenderingInServerForm" in our code otherwise we will get an Error Control of type "GridView" must be placed inside of the form tag with runat="server". Even we are placing a gridview in form which is running at server becausing we are calling the method RendorControl() manualy. Syntax to override the method is as follow
public override void VerifyRenderingInServerForm(Control control) { //Confirms that an HtmlForm control is rendered for the //specified ASP.NET //server control at run time. }
Step2:Assign datasource to gridview and set the allowpaging and allowsorting properties to false.
Ex:
public void ReportToExcel(GridView gridView,String fileName) { gridView.AllowPaging = false; gridView.AllowSorting = false; gridView.DataSource = resultDataSet; gridView.DataBind(); if (gridView != null && gridView.Rows.Count > 0) { PrepareGridViewForExport(gridView); ExportGridView(fileName,gridView); } }
Step3: Prepare the gridview to export. This step is requied only if the control otherthan literal used in the gridview.(Ex: if we use EditTemplate)
Sample code to Prepare Gridview For Export is as follows
public static void PrepareGridViewForExport(Control gvcontrol) { for (int i = 0; i < gvcontrol.Controls.Count; i++) { Control current = gvcontrol.Controls[i]; if (current is LinkButton) { gvcontrol.Controls.Remove(current); gvcontrol.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text)); } else if (current is ImageButton) { gvcontrol.Controls.Remove(current); gvcontrol.Controls.AddAt(i, new LiteralControl((current as ImageButton). AlternateText)); } else if (current is Image) { gvcontrol.Controls.Remove(current); gvcontrol.Controls.AddAt(i , new LiteralControl((current as Image). AlternateText)); } else if (current is HyperLink) { gvcontrol.Controls.Remove(current); gvcontrol.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text)); } else if (current is DropDownList) { gvcontrol.Controls.Remove(current); gvcontrol.Controls.AddAt(i, new LiteralControl((current as DropDownList). SelectedItem.Text)); } else if (current is CheckBox) { gvcontrol.Controls.Remove(current); gvcontrol.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True": "False")); } else if (current is TextBox) { gvcontrol.Controls.Remove(current); gvcontrol.Controls.AddAt(i, new LiteralControl((current as TextBox).Text)); } else if (current is HiddenField) { gvcontrol.Controls.Remove(current); } if (current.HasControls()) { PrepareGridViewForExport(current); } }
Step4: Export Grid view to Excel By using HtmlTextWriter. There is one method in gridview i.e, RenderControl(Writer)
public static void ExportGridView(string fileName, GridView gv) { HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response. AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName)); HttpContext.Current.Response.ContentType = "application/ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); gv.RenderControl(htw); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End(); }