Export an ASP.NET datagrid to HTML

Export with datagrid Render Control[-][--][++]

<codebox vbscript>
<%
'Dim stringWriter As System.IO.StringWriter = New System.IO.StringWriter
'Dim htmlWriter As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWriter)

'Must turn off sorting and paging
'Must also disable any custom buttons or links or image buttons in the datagrid, but I don't have any here
grdResults.AllowSorting = False
grdResults.AllowPaging = False

'Refresh the datagrid if it's not already
'The full bind here just gets the data from the database and calls the grid.Databind()
Me.dvResults = Nothing
Me.FullBind(Session("trkDsResults"))

'Render to HTML
grdResults.RenderControl(htmlWriter)

'Turn your stuff back on (and buttons, links... if any)
grdResults.AllowSorting = True
grdResults.AllowPaging = True

'Now stringWriter.ToString is your HTML datagrid output
'Write to a file here, if you want
Dim sWriter As System.IO.StreamWriter
sWriter = System.IO.File.CreateText(System.Configuration.ConfigurationSettings.AppSettings("FilePathABS").Trim & "Tracker/Temp/test.xls")
sWriter.Write(stringWriter.ToString)
sWriter.Close()
%>
</codebox>

Then you could stream the file to their browser with ASP.NET Stream File