Sunday 4 December 2011

export gridview to excel in asp.net(c#)

Introduction.


Here you find the code for how to export data from a gridview control to an excel.

.aspx file:-

<div>
<table>
<tr>
<td align="right">
<asp:ImageButton ID="btnExcel" runat="server" ImageUrl="~/ExcelImage.jpg"
onclick="btnExcel_Click" />

</td>

</tr>

<tr>

<td>
<asp:GridView runat="server" ID="grdexl" DataSourceID="dsdetails"  AllowPaging="true"AllowSorting="true" AutoGenerateColumns="false">
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField=" RollNo " HeaderText="RollNo" />
<asp:BoundField DataField=" Name " HeaderText="Name" />
<asp:BoundField DataField=" Class " HeaderText="Class" />
<asp:BoundField DataField=" Address " HeaderText="Address" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:cn %>"

SelectCommand="select * from UserInformation"/>
<div/>



You can bind Your Gridview using SqlDataSource or from code behind by creating your own bind method i.e in .aspx.cs file.
Public void gridbind()
{
  SqlDataAdapter adp=new SqlDataAdapter("\\Your Query",con);
  DataSet ds=new DataSet(); //You can use DataTable also........
  //pass parameter if required.......
  adp.Fill(ds);
  grdexl.DataSource=ds;
  grdexl.DataBinf();
}

But keep in mind that your connection string must be well defined in web.config file.


Now Add this

using System.IO;


Now write the following code in code behind



protected void btnExcel_Click(object sender, ImageClickEventArgs e)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition"string.Format("attachment; filename={0}""Add Filename.xls"));//add file name which you want to save your excel file.
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
grdexl.AllowPaging = false;
grdexl.DataBind();
grdexl.HeaderRow.Style.Add("background-color""#FFFFFF");/ //Applying style to Header Row

for (int i = 0; i <  grdexl .HeaderRow.Cells.Count; i++)//Applying style to gridview header cells
{
grdexl.HeaderRow.Cells[i].Style.Add("background-color""#507CD1");
}
int j = 1;
//This loop is used to apply stlye to cells based on particular row
foreach (GridViewRow grdrow in  grdexl .Rows)
{
grdrow.BackColor = Color.White;

//here applying different color to rows......
//its optional use it or not it is to give alternate color to rows......
if (j <=  grdexl .Rows.Count)
{
if (j % 2 != 0)
{
for (int k = 0; k <  grdrow.Cells.Count; k++)
{
grdrow.Cells[k].Style.Add("background-color""#EFF3FB");
}
}
}
j++;
}
grdexl.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}

public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}


Its done.Try it.
Good Luck.