Public Sub ExportDataTableToCSV(ByRef dtSource As DataTable, _
ByVal filename As String)
Dim context As HttpContext = HttpContext.Current
context.Response.Clear()
For Each column As DataColumn In dtSource.Columns
context.Response.Write(column.ColumnName + ",")
Next
context.Response.Write(Environment.NewLine)
For Each row As DataRow In dtSource.Rows
For i As Integer = 0 To dtSource.Columns.Count - 1
context.Response.Write(row(i).ToString().Replace(",", String.Empty) + ",")
Next
context.Response.Write(Environment.NewLine)
Next
context.Response.ContentType = "text/csv"
context.Response.AppendHeader("Content-Disposition", _
"attachment; filename=" + filename + ".csv")
context.Response.End()
End Sub
Monday, April 27, 2009
VB.Net - Export DataTable to CSV
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment