Home > Code Samples > Export DataTable to Excel in VB

Export DataTable to Excel



VB.Net
Dim products as New DataTable
...
Response.Clear()
Response.ClearHeaders()
Dim writer As New CsvWriter(Response.OutputStream, ","c, Encoding.Default)
writer.WriteAll(products, True)
writer.Close()
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "attachment; filename=products.csv")
Response.End()