Abhimanyu Singh

Abhimanyu Singh

  • NA
  • 116
  • 48.1k

Export to Excel in ASP .Net C#

Aug 8 2013 3:43 AM
Hii All here,

Please help me in exporting the Grid view row Data into excel with image....I have putted the following two alternate code Both are working for the 
same result but its not exporting all data like of next page data(paging index of grid view) also not showing image in Excel sheet.

Please help me...

1) Code one is : 


        protected void imgExportToExcel_Click(object sender, ImageClickEventArgs e)
        {

            Response.Clear();

            Response.AddHeader("content-disposition", "attachment; filename=PostedHoardingDetails.xls");

            Response.Charset = "";

            // If you want the option to "open only" the Excel file without "saving option"--then comment out the line below
            //Response.Cache.SetCacheability(HttpCacheability.NoCache);

            Response.ContentType = "application/vnd.xls";

            System.IO.StringWriter stringWrite = new System.IO.StringWriter();

            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

            gvPostedHoarding.RenderControl(htmlWrite);

            Response.Write(stringWrite.ToString());

            Response.End();
         

        }


        public override void VerifyRenderingInServerForm(Control control)
        {

            // Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.

        }



2) Alternate Code is : 



        protected void imgExportToExcel_Click(object sender, ImageClickEventArgs e)
        {

            
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=PostedHoardingDetails.xls");
            Response.Charset = "";            
            Response.ContentType = "application/vnd.ms-excel";
            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);
            gvPostedHoarding.RenderControl(hw);


            gvPostedHoarding.AllowPaging = false;
            gvPostedHoarding.DataBind();
            gvPostedHoarding.HeaderRow.Style.Add("background-color", "#FFFFFF");
            gvPostedHoarding.HeaderRow.Cells[0].Style.Add("background-color", "green");
            gvPostedHoarding.HeaderRow.Cells[1].Style.Add("background-color", "green");
            gvPostedHoarding.HeaderRow.Cells[2].Style.Add("background-color", "green");
            for (int i = 0; i < gvPostedHoarding.Rows.Count; i++)
            {
                GridViewRow row = gvPostedHoarding.Rows[i];
                row.BackColor = System.Drawing.Color.White;
                row.Attributes.Add("class", "textmode");
                if (i % 2 != 0)
                {
                    row.Cells[0].Style.Add("background-color", "#C2D69B");
                    row.Cells[1].Style.Add("background-color", "#C2D69B");
                    row.Cells[2].Style.Add("background-color", "#C2D69B");
                }
            }

            string style = @"<style> .textmode { mso-number-format:\@; } </style>";
            
            Response.Write(style);
            Response.Output.Write(sw.ToString());
            Response.End();

        }



        public override void VerifyRenderingInServerForm(Control control)
        {

            // Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.

        }


Please help me in exporting the all pages data into excel with image also...as it is currently exporting/showing first page data of grid veiw only.


Answers (1)