Wednesday, June 8, 2022

Export to excel ASP.NET C# GRID VIEW

 protected void Exportexcel(object sender, ImageClickEventArgs e)

    {

        Response.ClearContent();

        Response.Buffer = true;

        Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "BatchInformation.xls"));

        Response.ContentType = "application/ms-excel";

        StringWriter sw = new StringWriter();

        HtmlTextWriter htw = new HtmlTextWriter(sw);

        grdBatchList.AllowPaging = false;

       

        //Change the Header Row back to white color

        grdBatchList.HeaderRow.Style.Add("background-color", "#FFFFFF");

        //Applying stlye to gridview header cells

        for (int i = 0; i < grdBatchList.HeaderRow.Cells.Count; i++)

        {

            grdBatchList.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");

        }


        grdBatchList.RenderControl(htw);

        Response.Write(sw.ToString());

        Response.End();

       

    }

    public override void VerifyRenderingInServerForm(Control control)

    {

        /* Verifies that the control is rendered */

    }

No comments:

Post a Comment