Total Pageviews

Monday, 9 January 2012

How to Export ASP.NET GridView to MS EXCEL and MS Word


  #region Export
        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Verifies that the control is rendered */
        }
        /// <summary>
        /// This event is used to export gridview data to word document
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnWord_Click(object sender, ImageClickEventArgs e)
        {
            GridView1.AllowPaging = false;
            GridView1.AllowSorting = false;
            GridView1.Columns[6].Visible = false;
            GridView1.DataBind();
            Response.ClearContent();
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "ScratchCardNo.doc"));


            Response.ContentEncoding = Encoding.GetEncoding("ISO-8859-1"); //must
            Response.Charset = "ISO-8859-1"//optional
            Response.ContentType = "application/ms-word";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            GridView1.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }
        /// <summary>
        /// This Event is used to export gridview data to Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExcel_Click(object sender, ImageClickEventArgs e)
        {
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "ScratchCardNo.xls"));
            Response.ContentType = "application/ms-excel";
            Response.ContentEncoding = Encoding.GetEncoding("ISO-8859-1"); //must
            Response.Charset = "ISO-8859-1"; //optional
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            GridView1.AllowPaging = false;
            GridView1.AllowSorting = false;
            GridView1.Columns[6].Visible = false;
            GridView1.DataBind();
            ////Change the Header Row back to white color
            //GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
            ////Applying stlye to gridview header cells
            //for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
            //{
            //    GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
            //}
            //int j = 1;
            ////This loop is used to apply stlye to cells based on particular row
            //foreach (GridViewRow gvrow in GridView1.Rows)
            //{
            //    gvrow.BackColor = Color.White;
            //    if (j <= GridView1.Rows.Count)
            //    {
            //        if (j % 2 != 0)
            //        {
            //            for (int k = 0; k < gvrow.Cells.Count; k++)
            //            {
            //                gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
            //            }
            //        }
            //    }
            //    j++;
            //}
            GridView1.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }
        #endregion

Notes:
Add EnableEventValidation="false" to Page Property.
Avoid UpdatePanel in the Page.
if you use UpdatePanel then use

<Triggers>
   <asp:PostBackTrigger ControlID="btnWord" />
   <asp:PostBackTrigger ControlID="btnExcel" />
</Triggers>

Other  help:
Adding Serial Number to ASP.NET GridView
<asp:TemplateField HeaderText="S/N">
                            <ItemTemplate>
                                <asp:Label ID="lblNo" runat="server" Text='<%# Container.DataItemIndex + 1 %>' />
                            </ItemTemplate>
                        </asp:TemplateField>


No comments:

Post a Comment