Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition",
                               "attachment; filename=" + Session["NwAccountName"] + "_" + DateTime.Now +
                               "_DestinationUrlsReport.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            var sw = new StringWriter();
            var hw = new HtmlTextWriter(sw);
            GridView1.AllowPaging = false;
            GridView1.DataBind();
            GridView1.RenderControl(hw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
Posted
Updated 29-Sep-11 9:42am
v2
Comments
Pradeep Shukla 29-Sep-11 16:36pm    
What's the question ?
anuj_87 29-Sep-11 21:34pm    
I am using that above code and bind the data in GridView its returning empty error sheet.

I think you need to change the line as below
Response.ContentType = "application/ms-excel";

instead of
C#
Response.ContentType = "application/vnd.ms-excel";

and you need to add these lines
C#
Response.ContentEncoding = System.Text.Encoding.UTF8;
             Response.Cache.SetCacheability(HttpCacheability.NoCache);


and remove the
C#
Response.Flush();


Let me know if it works...
 
Share this answer
 
v2
The code seems to be ok, try these two things:

1- Comment the line - Response.Flush();
2- Use Response.Write(sw.ToString()); in place of
Response.Output.Write(sw.ToString());

Let me know how it goes...
 
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