Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Export Gridview to excel

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 143
    Comment on it

    In C#.net, GridView is use to display tabular data. With GridView it is not mandatory to specify column mappings. If not specified it automatically reads the column name from the data-table to which it is bound. However we must ensure that we specify a DataSource which is supports by the GridView.

    To Export GridView to excel

    We add button in .aspx page which contains the GridView for displaying data.By clicking this button user can export the GridView data to excel.

    Code for .aspx page

    <asp:GridView id="GridView1" runat="server" AutoGenerateColumns="true">
     </asp:GridView>
     <br />
    <asp:Button id ="btn" runat="server" Text="CreateExcelFile" OnClick = "button_Click" />

    Add the following Namespaces

    using System;

    using System.IO;

    using System.Data;

    using System.Data.SqlClient;

    using System.Configuration;

    using System.Web.UI;

    Binding the GridView

    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
       {
         this.BindGrid();
       }
    }
     
    private void BindGrid()
    {
    string conString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
       using (SqlConnection conn = new SqlConnection(conString))
        {
          conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT  [EmpID],[EmpName],[EmpDesignation] FROM EmployeeRecord"))
            {
              using (SqlDataAdapter da = new SqlDataAdapter())
              {
                    cmd.Connection = conn;
                    da.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        da.Fill(dt);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                    }
                }
            }
        }
    }
    
    protected void button_Click(object sender, EventArgs e)
     {
        Response.Clear();
        Response.AddHeader("content-disposition","attachment;filename=EmployeeData.xls");
        Response.ContentType = "application/excel";
        StringWriter strWriter = new StringWriter(); ;
        HtmlTextWriter HtmlWriter = new HtmlTextWriter(strWriter);
        GridView1.RenderControl(HtmlWriter);
        Response.Write(strWriter.ToString());
        Response.Flush();
        Response.End();
    }
    /* This method is use to verify the control is rendered*/ 
     public override void VerifyRenderingInServerForm(Control control)
       {
              
        }
    
    

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: