using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Security; using System.Collections; using System.Web.UI.WebControls.WebParts; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Web.UI.HtmlControls; using System.Data; using System.Configuration; using System.IO; public partial class Gridview_to_excel : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { bindgrid(); } private void bindgrid() { SqlConnection con = new SqlConnection("Data Source=........;Initial Catalog=.....;Integrated Security=True"); SqlDataAdapter da = new SqlDataAdapter("select * from gridview", con); DataSet ds = new DataSet(); da.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); } protected void btnexcel_Click(object sender, EventArgs e) { Response.Clear(); Response.AddHeader("content-dispotion", "attachment;filename=myexcelfile.xls"); Response.ContentType = "applicaton/vnd.xls"; StringWriter sw = new StringWriter(); HtmlTextWriter tw = new HtmlTextWriter(sw); GridView1.RenderControl(tw); Response.Write(sw.ToString()); Response.End(); } public override void VerifyRenderingInServerForm(Control control) { } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)