Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Export to Excel

Author  Topic 

BJM RAO
Starting Member

20 Posts

Posted - 2009-03-16 : 13:11:55
Wish to have a class to export GridView data in Excel format on click of a button in vb.net

kris22
Starting Member

35 Posts

Posted - 2009-03-19 : 17:52:20
add this subroutine
Private Sub ClearControls(ByVal control As Control)
Dim i As Integer
For i = control.Controls.Count - 1 To 0 Step -1
ClearControls(control.Controls(i))
Next i
If Not TypeOf control Is TableCell Then
If Not (control.GetType().GetProperty("SelectedItem") Is Nothing) Then
Dim literal As New LiteralControl
control.Parent.Controls.Add(literal)
Try
literal.Text = CStr(control.GetType().GetProperty("SelectedItem").GetValue(control, Nothing))
Catch
End Try
control.Parent.Controls.Remove(control)
Else
If Not (control.GetType().GetProperty("Text") Is Nothing) Then
Dim literal As New LiteralControl
control.Parent.Controls.Add(literal)
literal.Text = CStr(control.GetType().GetProperty("Text").GetValue(control, Nothing))
control.Parent.Controls.Remove(control)
End If
End If
End If
Return
End Sub

and in button click event add this code



Response.Clear()
Response.Buffer = True
Response.ContentType = "application/octet-stream"
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")
Response.Charset = ""
Me.EnableViewState = False
Dim oStringWriter As New System.IO.StringWriter
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
Me.ClearControls(datagrid)
datagrid.RenderControl(oHtmlTextWriter)
Response.Write(oStringWriter.ToString())
Response.End()
Go to Top of Page

BJM RAO
Starting Member

20 Posts

Posted - 2009-03-24 : 11:13:57
Thanks, purpose got solved earlier to ur reply
Go to Top of Page
   

- Advertisement -