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
"I have not failed. I've just found 10,000 ways that won't work."
--Thomas Edison
"Not everything that can be counted counts, and not everything that counts can be counted."
--Albert Einstein
"If you can't make it good, at least make it look good."
"Your most unhappy customers are your greatest source of learning."
--Bill Gates
No comments:
Post a Comment