jump to navigation

Exporing Datagrids to Excel June 21 2004

Thanks to DOT Net John for this one

http://www.dotnetjohn.com/articles/articleid78.aspx

Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
Response.Clear()
Response.Charset = “”
’set the response mime type for excel
Response.ContentType = “application/vnd.ms-excel”
‘create a string writer
Dim stringWrite As New System.IO.StringWriter
‘create an htmltextwriter which uses the stringwriter
Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)

‘instantiate a datagrid
Dim dg As New DataGrid
‘ just set the input datagrid = to the new dg grid
dg.DataSource = Session(”UserSpreadsheet_dv”)

‘ I want to make sure there are no annoying gridlines
‘dg.GridLines = GridLines.None
‘ Make the header text bold
dg.HeaderStyle.Font.Bold = True

‘ If needed, here’s how to change colors/formatting at the component level
‘dg.HeaderStyle.ForeColor = System.Drawing.Color.Black
‘dg.ItemStyle.ForeColor = System.Drawing.Color.Black

‘bind the modified datagrid
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

Comments»

no comments yet - be the first?