Below code is
used for export selected row from GridView to Excel .Here you can find code for
DataTable to Excel.
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
public partial class _Default :
System.Web.UI.Page
{
DataTable
dt = new DataTable();
DataTable
dt1 = new DataTable();
DataRow
dr;
DataRow
dr1;
protected void
Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}
void BindGrid()
{
dt.Columns.Add("eno");
dt.Columns.Add("empname");
dt.Columns.Add("sal");
dr =
dt.NewRow();
dr["eno"] = "101";
dr["empname"] = "Ravindran";
dr["sal"] = "45000";
dt.Rows.Add(dr);
dr =
dt.NewRow();
dr["eno"] = "102";
dr["empname"] = "James";
dr["sal"] = "35000";
dt.Rows.Add(dr);
dr =
dt.NewRow();
dr["eno"] = "103";
dr["empname"] = "Mike";
dr["sal"] = "20000";
dt.Rows.Add(dr);
if (dt.Rows.Count > 0)
{
Session["source_table"] =
dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
//Export all records to excel
protected void
Button1_Click(object sender, EventArgs e)
{
//here session value is stored before bind data on gridview
DataTable dt = Session["source_table"]
as DataTable;
ExportExcel(dt);
}
void ExportExcel(DataTable dt)
{
Response.ClearContent();
Response.AddHeader("content-disposition",
"attachment; filename=" + DateTime.Now.ToString("ddMMyyyy")
+ ".xls");
Response.ContentType = "application/ms-excel";
string tab = "";
foreach (DataColumn dc in
dt.Columns)
{
Response.Write(tab + dc.ColumnName);
tab = "\t";
}
Response.Write("\n");
int i;
foreach (DataRow dr in
dt.Rows)
{
tab = "";
for (i = 0; i < dt.Columns.Count; i++)
{
Response.Write(tab + dr[i].ToString());
tab = "\t";
}
Response.Write("\n");
}
Response.End();
}
protected void
GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if ((e.Row.RowType == DataControlRowType.Header))
{
((CheckBox)e.Row.FindControl("SelectAll")).Attributes.Add("onclick", "javascript:SelectAll('"
+ ((CheckBox)e.Row.FindControl("SelectAll")).ClientID
+ "')");
}
}
//Export only selected record
protected void
Button2_Click(object sender, EventArgs e)
{
int k = 0;
//Checkther whether atleast one check box is selected or
not
for (int i = 0; i
<= GridView1.Rows.Count - 1; i++)
{
GridViewRow row = GridView1.Rows[i];
CheckBox Ckbox = (CheckBox)row.FindControl("CheckBox1");
if (Ckbox.Checked == true)
{
k++;
}
}
if (k == 0)
{
Page.RegisterStartupScript("Alert
Message", "");
return;
}
dt1.Columns.Add("eno");
dt1.Columns.Add("empname");
dt1.Columns.Add("sal");
for (int i = 0; i
<= GridView1.Rows.Count - 1; i++)
{
GridViewRow row = GridView1.Rows[i];
CheckBox Ckbox = (CheckBox)row.FindControl("CheckBox1");
if (Ckbox.Checked == true)
{
dr1 = dt1.NewRow();
dr1["eno"] =
GridView1.Rows[i].Cells[1].Text;
dr1["empname"] =
GridView1.Rows[i].Cells[2].Text;
dr1["sal"] =
GridView1.Rows[i].Cells[3].Text;
dt1.Rows.Add(dr1);
}
}
ExportExcel(dt1);
}
}