Click here to Skip to main content
14,882,280 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I need to export the nested grid to excel format.

anybody having an idea or code.

Help pl

Regards
Praveen
Posted

Hi,Praveen..

Use Following Function Just Pass Grid-view ID which is to export..... and name To Excel Sheet as Parameters to following Function..

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


       }
   
Comments
praveena.karthi 1-Nov-11 5:58am
   
Hello Tushar Patil,

I try the above code, but its not working fine. In my child Gridview Its having a heading may due to this. it may not be working. Can you give me any other coding to do the exporting to excel

Thanks

Regards,
Praveen
prashant kolekar 3-Dec-11 8:00am
   
the above is working fine
_Tushar Patil 1-Nov-11 6:08am
   
what it is can you elaborate .....?
prashant kolekar 3-Dec-11 8:01am
   
good article
Md Aamir Iqbal 20-Sep-12 2:36am
   
"PrepareControlForExport does not exist in the current context" i am getting this error! Where it is defined?
Niranjan dotnet 29-Oct-18 9:20am
   
Where is PrepareControlForExport method ?

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