The following Class can be used to export a dataset to XLS format. This code has been tested with Office 2002 and XP:
Public Class DataSetToExcel
Public Shared Sub Export(ByVal ds As DataSet, ByVal response As HttpResponse)
'need to clear the response object
response.Clear()
response.Charset = ""
'set the response type to excel
response.ContentType = "application/vnd.ms-excel"
'create a string writer anf HTMLTextWriter that uses it
Dim StringWrite As New System.IO.StringWriter
Dim HTMLWrite As New System.Web.UI.HtmlTextWriter(StringWrite)
'instantiate a datagrid
Dim dg As New DataGrid
'set the datagrid datasource to the dataset passed in and bind it
dg.DataSource = ds.Tables(0)
dg.DataBind()
'tell the datagrid to render itself to our htmltextwriter
dg.RenderControl(HTMLWrite)
'output the html
response.Write(StringWrite.ToString)
response.End()
End Sub
End Class