Writing CSV file on the fly
Read the post here. There is also the code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Response.ContentType = ""
Response.AppendHeader("Content-Disposition", "inline;filename=filename.csv")
Response.Write(GetCSV(myDataTable))
Response.End()
End Sub
Private Function GetCSV(ByVal dt As DataTable) As String
Dim sb As New System.Text.StringBuilder
Dim rowCounter, colCounter As Integer
'first the row with fieldnames
For colCounter = 0 To dt.Columns.Count - 1
sb.Append(Chr(34))
sb.Append(dt.Columns(colCounter).ColumnName)
sb.Append(Chr(34))
sb.Append(",")
Next
sb.Append(vbCrLf)
'then the rows of data
For rowCounter = 0 To dt.Rows.Count - 1
For colCounter = 0 To dt.Columns.Count - 1
If Not IsDBNull(dt.Rows(rowCounter).Item(colCounter)) Then
If Not IsNumeric(dt.Rows(rowCounter).Item(colCounter)) Then
sb.Append(Chr(34))
End If
sb.Append(dt.Rows(rowCounter).Item(colCounter))
If Not IsNumeric(dt.Rows(rowCounter).Item(colCounter)) Then
sb.Append(Chr(34))
End If
Else
'sb.Append(Chr(34) & Chr(34))
End If
If colCounter <> dt.Columns.Count - 1 Then
sb.Append(",")
End If
Next
sb.Append(vbCrLf)
Next
Return sb.ToString
End Function