I am trying to create a csv file of some data. I have wrote a function that successfully does this....
Private Sub CreateCSVFile(ByVal dt As DataTable, ByVal strFilePath As String)
Dim sw As New StreamWriter(strFilePath, False)
''# First we will write the headers.
''#DataTable dt = m_dsProducts.Tables[0];
Dim iColCount As Integer = dt.Columns.Count
For i As Integer = 0 To iColCount - 1
sw.Write(dt.Columns(i))
If i < iColCount - 1 Then
sw.Write(",")
End If
Next
sw.Write(sw.NewLine)
''# Now write all the rows.
For Each dr As DataRow In dt.Rows
For i As Integer = 0 To iColCount - 1
If Not Convert.IsDBNull(dr(i)) Then
sw.Write(dr(i).ToString())
End If
If i < iColCount - 1 Then
sw.Write(",")
End If
Next
sw.Write(sw.NewLine)
Next
sw.Close()
End Sub
The problem is I am not using the streamwriter object correctly for what I trying to accomplish. Since this is an asp.net I need the user to pick a local filepath to put the file on. If I pass any path to this function its gonna try to write it to the directory specified on the server where the code is. I would like this to popup and let the user select a place on their local machine to put the file....
Dim exData As Byte() = File.ReadAllBytes(Server.MapPath(eio))
File.Delete(Server.MapPath(eio))
Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", fn))
Response.ContentType = "application/x-msexcel"
Response.BinaryWrite(exData)
Response.Flush()
Response.End()
I am calling the first function in code like this...
Dim emplTable As DataTable = SiteAccess.DownloadEmployee_H()
CreateCSVFile(emplTable, "C:\\EmplTable.csv")
Where I dont want to have specify the file loaction (because this will put the file on the server and not on a client machine) but rather let the user select the location on their client machine.
Can someone help me put this together? Thanks in advance.
I have recreated my export function and now it lets the usr select a download path, but one column in the data being downloaded has data in the form of "Doe, John" this column is called "EPLNME" this messes up the output file because its reading the comma in the data and now the data is off by a column in the output file can someone help me stop this specific incident im not sure how I can. Here is the code...
Private Sub ExportCSV(ByVal data As DataTable, ByVal nameOfFile As String)
Dim context As HttpContext = HttpContext.Current
context.Response.Clear()
context.Response.ContentType = "text/csv"
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + nameOfFile + ".csv")
''#Write column header names
For i = 0 To data.Columns.Count - 1
If (i > 0) Then
context.Response.Write(",")
End If
context.Response.Write(data.Columns(i).ColumnName)
Next
context.Response.Write(Environment.NewLine)
''#Write data
For Each row As DataRow In data.Rows
For i = 0 To data.Columns.Count - 1
If (i > 0) Then
context.Response.Write(",")
End If
context.Response.Write(row.Item(i).ToString())
Next
context.Response.Write(Environment.NewLine)
Next
context.Response.End()
End Sub