Hello everyone.
I am trying to export the contents of a DataGrid to MS Excel using VB.NET 2003. I read of a couple different ways of doing that here (though I couldn't tell which version of .NET was the subject of the article):
http://www.aspnetpro.com/NewsletterArticle/2003/09/asp200309so_l/asp200309so_l.asp
The following code snippet from that article DOES work for me:
-----------------------------------------------------------------------------
'Converts a DataTable to Tab Delimited Format
Private Function ConvertDtToTDF(ByVal dt As DataTable) As String
Dim dr As DataRow, ary() As Object, i As Integer
Dim iCol As Integer
'Output Column Headers
For iCol = 0 To dt.Columns.Count - 1
Response.Write(dt.Columns(iCol).ToString & vbTab)
Next
Response.Write(vbCrLf)
'Output Data
For Each dr In dt.Rows
ary = dr.ItemArray
For i = 0 To UBound(ary)
Response.Write(ary(i).ToString & vbTab)
Next
Response.Write(vbCrLf)
Next
End Function
-----------------------------------------------------------------------------
But what I really want to do is to export HTML, not just in tab delimited format. The following code snippet from that article DOES NOT work for me.
-----------------------------------------------------------------------------
Protected Overrides Sub Render(ByVal hWriter As System.Web.UI.HtmlTextWriter)
'Create and populate a DataTable
Dim dt As DataTable = New DataTable()
dt = MakeTable(dt)
Response.ContentType = "application/ms-excel"
Response.AddHeader("Content-Disposition", "inline;filename=Process_Aspect_List_By_Process.xl s")
DataGrid1.DataSource = dt
DataGrid1.DataBind()
DataGrid1.RenderControl(hWriter)
End Sub
-----------------------------------------------------------------------------
A blank page or blank excel spreadsheet (depending on wheither I define the Response.AddHeader with 'inline' or 'attachment') appears when the latter code is run.
I have tried every version and permutation of this idea that I can find on the web, but none of them working. Anyone have any ideas on why it isn't working or what I might need to change?
Much appreciated!
-Christine