Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to export gridview data to excel but when i open that excel sheet,it shows nothing. I am using the following code
C#
protected void btnExport_Click(object sender, EventArgs e)
       {
           Response.ClearContent();
           Response.Buffer = true;
           Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
           Response.ContentType = "application/ms-excel";
           System.IO.StringWriter sw = new System.IO.StringWriter();
           HtmlTextWriter htw = new HtmlTextWriter(sw);
           gvDetails.AllowPaging = false;
           gvDetails.DataBind();
           //Change the Header Row back to white color
           gvDetails.HeaderRow.Style.Add("background-color", "#FFFFFF");
           //Applying stlye to gridview header cells
           for (int i = 0; i < gvDetails.HeaderRow.Cells.Count; i++)
           {
               gvDetails.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 gvDetails.Rows)
           {
               if (j <= gvDetails.Rows.Count)
               {
                   if (j % 2 != 0)
                   {
                       for (int k = 0; k < gvrow.Cells.Count; k++)
                       {
                           gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                       }
                   }
               }
               j++;
           }
           gvDetails.RenderControl(htw);
           Response.Write(sw.ToString());
           Response.End();
       }
   }

I have also applied the verify rendering in server form method and have set the EnableEventValidation to false, but it in the excel sheet i did not get any data.
Please help. All the columns in gridview are Template Fields.
Posted
Updated 11-Nov-11 19:19pm
v2
Comments
Wayne Gaylard 12-Nov-11 1:20am    
Put code in code blocks and removed bold (this is considered shouting on internet forums and is rude).

1 solution

Try Following Code It IS Working For Me...............

C#
public static void ExportToExcel(GridView gv, string filename)
       {
           HttpContext.Current.Response.Clear();
           HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", filename));
           HttpContext.Current.Response.ContentType = "application/ms-excel";

           using (StringWriter sw = new StringWriter())
           {
               using (HtmlTextWriter htw = new HtmlTextWriter(sw))
               {
                   //  Create a table to contain the grid
                   Table table = new Table();

                   //  include the gridline settings
                   table.GridLines = gv.GridLines;

                   //  add the header row to the table
                   if (gv.HeaderRow != null)
                   {
                      PrepareControlForExport(gv.HeaderRow);
                       table.Rows.Add(gv.HeaderRow);
                   }
                   //Make Header Coloruful

                   for (int j = 0; j < gv.Columns.Count; j++)
                   {
                       //Apply style to Individual Cells
                       gv.HeaderRow.Cells[j].Style.Add("background-color", "yellow");
                   }

                   //  add each of the data rows to the table
                   foreach (GridViewRow row in gv.Rows)
                   {
                      PrepareControlForExport(row);
                       table.Rows.Add(row);
                   }

                   //  add the footer row to the table
                   if (gv.FooterRow != null)
                   {
                       PrepareControlForExport(gv.FooterRow);
                       table.Rows.Add(gv.FooterRow);
                   }

                   //  render the table into the htmlwriter
                   table.RenderControl(htw);

                   //  render the htmlwriter into the response
                   HttpContext.Current.Response.Write(sw.ToString());
                   HttpContext.Current.Response.End();
               }
           }


       }


To Export Data From Gridview........
Call Above Function..as
ExportToExcel(grd_data, "abc.xls")
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900