Hiii Clev
Get Complete Code From here
<div>
<asp:LinkButton ID="excel" runat="server" onclick="excel_Click" >excel </asp:LinkButton>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Export in ms Word</asp:LinkButton>
</div>
<div>
<asp:GridView ID="GridView1" runat="server" Width="500px" >
</asp:GridView>
</div>
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
dt.Rows.Add(1, "Fani Mohanthy", "India");
dt.Rows.Add(2, "KALEEM IQBAL", "India");
dt.Rows.Add(3, "RAHUL SHARMA", "India");
dt.Rows.Add(4, "Surendra Singh", "India");
dt.Rows.Add(5, "Santosh Kushwaha", "India");
dt.Rows.Add(6, "Amit Mishra", "India");
dt.Rows.Add(7, "Amit Mathur", "India");
dt.Rows.Add(8, "Swapnil Chaturvedi", "India");
ViewState["dt"] = dt;
this.BindGrid();
}
protected void BindGrid()
{
GridView1.DataSource = ViewState["dt"] as DataTable;
GridView1.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
try
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.doc");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-word ";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
BindGrid();
GridView1.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
catch (Exception ex)
{
}
finally
{
}
}
protected void excel_Click(object sender, EventArgs e)
{
try
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=griddata.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages
GridView1.AllowPaging = false;
this.BindGrid();
//CheckBoxList chk = (CheckBoxList)this.griddata.FindControl("CheckBoxList1");
//chk.Visible = false;
GridView1.HeaderRow.BackColor = System.Drawing.Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = System.Drawing.Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
/* Verifies that the control is rendered */
}
}
catch (Exception ex)
{
}
finally
{
}
}
}
This will full fill your require ment if you want to show data from sql server then only bind gridview using database and all code will be unchanged