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

Monday, November 3, 2014

DataGrid Export to Excel

//btnExporttoExcel
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
string FileName = "MS EXCEL" + DateTime.Now + ".xls";
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
dgpayment.GridLines = GridLines.Both;
dgpayment.HeaderStyle.Font.Bold = true;
dgpayment.RenderControl(htmltextwrtter);
Response.Write(strwritter.ToString());
Response.End();

//another method

public override void VerifyRenderingInServerForm(Control control)
        {
            //
        }

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();