Export data of DataGrid to Excel file using VB.NET
'Exports DataGrid1 to excel
Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'clears HTML output buffer
Response.Clear()
'specify what type of filename is to export
Response.AddHeader("content-disposition", "attachment;filename=Syntaxhelp.xls")
'speficy characterset name
Response.Charset = ""
'removes cache
Response.Cache.SetCacheability(HttpCacheability.NoCache)
'specify type of file to export
Response.ContentType = "application/vnd.xls"
Dim str As New System.IO.StringWriter()
Dim htmlWriter As New HtmlTextWriter(str)
'writes data to HTML text writer
DataGrid1.RenderControl(htmlWriter)
'writes the file
Response.Write(str.ToString())
Response.End()
End Sub