How to Export Table Values In
Asp.net into excel/gridview to excel
protected void
btnExport_Click(object sender, EventArgs e)
{
DataTable dt =codeForgridbind
if (dt.Rows.Count > 0)
{
gvSuscribers.DataSource = dt;
gvSuscribers.DataBind();
}
else
{
idWarning.Visible = true;
}
GridView GridView1 = new GridView();
GridView1.AutoGenerateColumns = true;
dt.Columns.Remove("Suscribers_Id");
dt.Columns.Remove("status");
dt.AcceptChanges();
GridView1.DataSource = dt;
GridView1.DataBind();
Response.ContentType =
"application/excel";
Response.AddHeader("content-disposition",
"attachment;filename='yourfilename'.xls");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
Response.Write(sw.ToString());
Response.End();
}
Comments
Post a Comment
Thank you for your Comment