Tuesday, September 27, 2016

Export ASPX GridView control to Excel

GridView control in aspx page
Report.aspx
<asp:Button ID="btnExport" runat="server" Text="Export To Excel" OnClick ="btnExport_Click" />
            <br /><br />
            <asp:GridView ID="GridView1" PageSize="5" AllowPaging="True" DataKeyField="PSID" OnRowDataBound="RowDataBound" AutoGenerateColumns="False" runat="server">
                <Columns>
                    <asp:BoundField DataField="PSID" HeaderText="PSID" SortExpression="PSID"></asp:BoundField>
                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"></asp:BoundField>
                    <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email"></asp:BoundField>
                    <asp:BoundField DataField="Area" HeaderText="Area" SortExpression="Area"></asp:BoundField>
                    <asp:BoundField DataField="Department" HeaderText="Department" SortExpression="Department"></asp:BoundField>               
                    <asp:BoundField DataField="Attempted" HeaderText="No of Attemped" SortExpression="Attempted"></asp:BoundField>
                    <asp:BoundField DataField="AcceptedStatus" HeaderText="Status" SortExpression="AcceptedStatus"></asp:BoundField>
                </Columns>
            </asp:GridView>


Report.aspx.cs

 protected void Page_Load(object sender, EventArgs e)
        {

            using (SqlConnection con = new SqlConnection(conString))
            {

             if(!IsPostBack){
                 Status.Items.Add(new ListItem("All Status", ""));
                Status.Items.Add(new ListItem("Accepted", "1"));
                Status.Items.Add(new ListItem("Decline", "0"));
               }


                using (SqlCommand cmd = new SqlCommand("SELECT * FROM TBL_TRN_REPORT", con))
                {
                    cmd.CommandType = CommandType.Text;
                    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    {
                        using (DataTable dt = new DataTable())
                        {
                            sda.Fill(dt);
                            GridView1.DataSource = dt;
                            GridView1.DataBind();
                        }
                    }
                }
            }
        }

 protected void btnExport_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xlsx");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            GridView1.RenderControl(hw);
            Response.Write(sw.ToString());
            Response.End();
           
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
            //It solves the error "Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server."
        }

No comments:

Post a Comment