views:

122

answers:

3

hi i am developing an application where i am using a grid to dispaly data and adding a dynamic datatable as the header of the gridview and iam using the following code to export the grid view into excel but i am unable get the datatable which was added dynamically to the grid into the excel. the code i am using is:

    Response.AddHeader("content-disposition", "attachment;filename=Report.xls");
    Response.Charset = String.Empty;
    Response.ContentType = "application/vnd.xls";
    System.IO.StringWriter sw = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView1.DataBind();
    GridView1.RenderControl(hw);
    Response.Write(sw.ToString());
    Response.End();

is thee any other method to follow.

Thanks in advance!

+1  A: 

chekout this link

http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html

Prashant
i have already tried this one but the headers that are added dynamically using datatable are not being exported.
A: 

Maybe a silly question but would writing to a csv fit your requirements?

http://www.codekeep.net/snippets/711e39f5-717b-44b1-bbd7-9e00ec34a463.aspx

It still could be content type excel too if you needed.

ElvisLives
A: 

I don't see where you are adding the dynamic datatable as the header?? This should come after the databind and before the GridView1.RenderControl(hw);

JBrooks