Showing posts with label child grid export to excel. Show all posts
Showing posts with label child grid export to excel. Show all posts

Thursday, January 30, 2014

Export to Excel with child grid in .Net

DataSet dsParent = new System.Data.DataSet();
            //export to excel with allow paging =false
            Response.Clear();
            //parent grid
            grvFetchJobPosting.AllowPaging = false;
            Response.Buffer = true;
            GridView gvrChild = new GridView();
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            dsParent = (DataSet)Session["FetchVMSData"];
            grvFetchJobPosting.AllowPaging = false;
            grvFetchJobPosting.DataSource = dsParent.Tables[0];
            grvFetchJobPosting.DataBind();

            HtmlForm frm = new HtmlForm();
            foreach (GridViewRow gvr in grvFetchJobPosting.Rows)
            {
                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    GridView GridView2 = (GridView)gvr.FindControl("grvVendorData");//child grid view
                    GridView2.AllowPaging = false;
                }
            }

            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", "attachment;filename=SalesReport.xls");
            //Response.Headers["Content-Disposition"] = "attachment;filename=DetailedRRFReport.xls";
            Response.Charset = "";
            this.EnableViewState = false;
            this.ClearControls(grvFetchJobPosting);
            grvFetchJobPosting.RenderControl(oHtmlTextWriter);

            grvFetchJobPosting.Parent.Controls.Add(frm);
            frm.Attributes["runat"] = "server";
            //frm.Controls.Add(FetchRRFDataGrid);
            frm.RenderControl(oHtmlTextWriter);

            Response.Write(oStringWriter.ToString());
            Response.End();