Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Exporting Page content to Excel in asp.net where data is generated dynamically.
Posted

1 solution

Assuming your data is generated in Gridview,then pass that gridview to this function.

C#
protected void btnExcel_Click(object sender, ImageClickEventArgs e)
        {
            GenerateExcel(GridView);
        }

public static void GenerateExcel(GridView gv)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment; filename=Leave_Report.xls");
            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", "#4DB4EE");

                    }

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


        }
        private static void PrepareControlForExport(Control control)
        {
            for (int i = 0; i < control.Controls.Count; i++)
            {
                Control current = control.Controls[i];
                if (current is LinkButton)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
                }
                else if (current is ImageButton)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
                }
                else if (current is HyperLink)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
                }
                else if (current is DropDownList)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
                }
                else if (current is CheckBox)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
                }

                if (current.HasControls())
                {
                    PrepareControlForExport(current);
                }
            }
        }
public override void VerifyRenderingInServerForm(Control control)
        {
            /* Verifies that the control is rendered */
        }
 
Share this answer
 
v2
Comments
apr1234 6-May-13 5:28am    
Thanks for the update Rohan....
Can you help me in getting data when we have other controls like radio buttons, multi line text boxes and other controls except gridview.....
apr1234 6-May-13 6:24am    
Actually it did not solve my question. I am pointing out that their is not grid in the page content.
Thanks7872 6-May-13 6:26am    
You have still not mentioned which control did you used or what you want to export?I have provided you with the example.
apr1234 6-May-13 6:58am    
I have a page in which i am using following list of controls:
1. Radio buttons.
2. Text box
3. Multi line text box
4. Image
5. Literal
These are the controls which i am expecting while page contents are exported to excel.
Thanks7872 6-May-13 7:05am    
Have you tried working lil bit with the above PrepareControlForExport function?if you did,you would have found the solution.

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