Sunday, August 12, 2018

Export Gridview to Excel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;
using System.Web.UI.HtmlControls;



protected void Button2_Click(object sender, EventArgs e)
    {
        string attachment = "attachment; filename=PROJECTRRCREPORT.xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        // HttpContext.Current.Response.Write("<img src='http://ghrce.edu.in/pic/adminlogin.JPG'>");
        //HttpContext.Current.Response.Write("<br><br><br><br><br><br><br>");     
        HttpContext.Current.Response.Write("<br>");
        // Create a form to contain the grid
        HtmlForm frm = new HtmlForm();
        GridView2.Parent.Controls.Add(frm);
        frm.Attributes["runat"] = "server";
        frm.Controls.Add(GridView2);
        frm.RenderControl(htw);
        //GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }

No comments:

Post a Comment

GRIDVIEW ON ROW DATA BOUND EVENT

 Database Create  Student : roll , name , city , cost  Fix 6 Value  in Database Record  ====================================================...