DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Export Data Fro Grid View To Excel In Asp.net

01.28.2012
| 3865 views |
  • submit to reddit
        // export data fro grid view to excel in asp.net

 Private Sub ExportDataToExcel(ByVal myDataTable As DataTable)
        Dim fileName As String = ""
        Response.Clear()
        Response.ContentType = "application/vnd.ms-excel"
        fileName = "MyFile"
        Response.AddHeader("Content-Disposition", "filename=" & fileName & DateTime.Now.Ticks & ".xls")
        Response.Charset = ""
        Dim sw As New StringWriter
        Dim htw As New HtmlTextWriter(sw)
        Dim myGrid As New GridView
        myGrid.AllowPaging = False
        myGrid.DataSource = myDataTable 
        myGrid.DataBind()
                myGrid.RenderControl(htw)
                Response.Write(sw.ToString())
        Response.End()
        myGrid.Dispose()
    End Sub