Showing posts with label How to export GridView to Excel. Show all posts
Showing posts with label How to export GridView to Excel. Show all posts

Tuesday, September 9, 2008

ASP.Net 2.0: Export GridView to Excel + c#


I faced a problem yesterday where I have to export a gridview content to excel. The problem was as follows :


User is genearating some reports –those reports are displaying in grdviews and he also wants to download these reports into a xls file. So here is the solution-



I cerated a class GridViewExportToExcel,there is a method: Export which will perform the necessary task. GridViewExportToExcel’s code is given below :-


using System;

using System.Data;


using System.Configuration;


using System.IO;


using System.Web;


using System.Web.Security;


using System.Web.UI;


using System.Web.UI.WebControls;


using System.Web.UI.WebControls.WebParts;


using System.Web.UI.HtmlControls;



public class GridViewExportToExcel


{


public static void Export(string fileName, GridView gv)


{


HttpContext.Current.Response.Clear();



//Export will take two parameter first one the name of Excel File, and second one for gridview to be exported
HttpContext.Current.Response.AddHeader(


"content-disposition", string.Format("attachment; filename={0}", fileName));


HttpContext.Current.Response.ContentType = "application/octet-stream";



using (StringWriter strWriter = new StringWriter())


{


using (HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter))


{


// Create a form to contain the grid


Table table = new Table();



// add the header row to the table


if (gv.HeaderRow != null)


{


GridViewExportToExcel.ExportControl(gv.HeaderRow);


table.Rows.Add(gv.HeaderRow);


}



// add each of the data rows to the table


foreach (GridViewRow row in gv.Rows)


{


GridViewExportToExcel.ExportControl(row);


table.Rows.Add(row);


}



// add the footer row to the table


if (gv.FooterRow != null)


{


GridViewExportToExcel.ExportControl(gv.FooterRow);


table.Rows.Add(gv.FooterRow);


}



// render the table into the htmlwriter


table.RenderControl(htmlWriter);



// render the htmlwriter into the response


HttpContext.Current.Response.Write(strWriter.ToString());


HttpContext.Current.Response.End();


}


}


}



/// Replace controls with literals


private static void ExportControl(Control control)


{


for (int i = 0; i < control.Controls.Count; i++)


{


Control current = control.Controls[i];


if (current is LinkButton)


{


control.Controls.Remove(current);


control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));


}


else if (current is ImageButton)


{


control.Controls.Remove(current);


control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));


}


else if (current is HyperLink)


{


control.Controls.Remove(current);


control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));


}


else if (current is DropDownList)


{


control.Controls.Remove(current);


control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));


}


else if (current is CheckBox)


{


control.Controls.Remove(current);


control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));


}


//Like that you may convert any control to literals


if (current.HasControls())


{


GridViewExportToExcel.ExportControl(current);


}


}


}


}




Using the above code you may export any data representation control’s data to Excel.
Leave comments if its help you and your suggestion are also welcome.
Happy Programming….:)