.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Export to excel .net grid

Posted By:Maverick       Posted Date: March 25, 2011    Points: 40    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

Export to excel
 

 Private Sub ExportToExcel(ByVal dt As DataTable)
        Dim strFileName As String = ""
        Response.Clear()
        Response.ContentType = "application/vnd.ms-excel"
        strFileName = "BatchPayments"
        Response.AddHeader("Content-Disposition", "filename=" & strFileName & DateTime.Now.Ticks & ".xls")
        Response.AddHeader("Refresh", "1;URL=Payment.aspx?s=e")
        Response.Charset = ""
        Dim sw As New StringWriter
        Dim htw As New HtmlTextWriter(sw)
        Dim gv As New GridView
        gv.AllowPaging = False
        gv.DataSource = dt
        gv.DataBind()
        For i As Integer = 0 To 5
            gv.HeaderRow.Cells(i).Style.Add("background-color", "#00FF00")
        Next
        gv.HeaderRow.Cells(4).Text = ""
        gv.HeaderRow.Cells(5).Text = "Description"
        gv.RenderControl(htw)
                Response.Write(sw.ToString())
        Response.End()
        gv.Dispose()
    End Sub


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend