2

I am trying to export a GridView to Excel.

I have tried to follow steps found here:

  1. http://www.programming-free.com/2012/09/aspnet-export-grid-view-to-excel.html#.UhUREpK1F9o
  2. export gridview to excel file

And other similar sites.

My GridView does not have any special properties different from default and my SqlDataSource uses filterExpression if that is important.

When I try the above mentioned solutions no exception occurs, but the excel is not produced.

UPDATE

I forgot to mention that the GridView is inside a asp:Content control. I heard this might matter.

My code-behind goes something like this ( I have tried multiple things ).

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = String.Empty;
EnableViewState = false;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView3.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());         
HttpContext.Current.ApplicationInstance.CompleteRequest(); 
Community
  • 1
  • 1
coredump
  • 3,017
  • 6
  • 35
  • 53
  • How are you trying to export it? Show at least the codebehind. Have you used the debugger to see what happens? – Tim Schmelter Aug 21 '13 at 21:35
  • @ I have used the debugger and like I said, no exception occurs, everything runs smooth, except the page is not displayed. – coredump Aug 21 '13 at 21:38
  • But this method is called, isn't it? Also, i assume that you have implemented [`VerifyRenderingInServerForm`](http://stackoverflow.com/questions/6343630/gridview-must-be-placed-inside-a-form-tag-with-runat-server-even-after-the-gri/6343859#6343859). – Tim Schmelter Aug 21 '13 at 21:50
  • @TimSchmelter Yes, it is called on a button click. The button also resides in the `asp:Content` but NOT in the `GridView` – coredump Aug 21 '13 at 21:51
  • So the button is in a GridView(where exactly?) which sits in a MasterPage's `Content`? Have you seen my question about `VerifyRenderingInServerForm`? You are not using ASP.NET-Ajax, are you? – Tim Schmelter Aug 21 '13 at 21:53
  • I am using some Ajax. In what way does this matter ? – coredump Aug 21 '13 at 21:55
  • You have to use a `PostBackTrigger` for the button instead of an asynchronous postback if it's in an `UpdatePanel`. – Tim Schmelter Aug 21 '13 at 21:56
  • @TimSchmelter So to make it clear. My button is NOT inside the `GridView`, but both the button and the `GridView` are inside the `Content` control that is rendered inside the `Master Page`. And yes, I have implemented ( empty impl ) for `VerifyRenderingInServerForm`. – coredump Aug 21 '13 at 22:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35951/discussion-between-coredump-and-tim-schmelter) – coredump Aug 21 '13 at 22:09

1 Answers1

0

You can create a GridView on the code behind and select the data seperately and export that Gridview as follows. So You don't have to worry about the Gridview on the Page.

Dim GridView1 As New GridView

SqlDataSource1.SelectCommand = "SELECT * FROM TableName"
GridView1.DataSource = SqlDataSource1
GridView1.DataBind()

Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Me.EnableViewState = False
Dim oStringWriter As New System.IO.StringWriter
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)

GridView1.RenderControl(oHtmlTextWriter)

Response.Write(oStringWriter.ToString())
Response.End()
Cherian M Paul
  • 646
  • 4
  • 11