mcdonnc2004
05-01-2007, 04:14 AM
Hi all,
I am getting slightly frustrated with this problem now and everthing I try does not work! I have some code that selects data from a SQL table and stores it in a DataTable. I then loop through this DataTable to write out the data as CSV. The problem is that I have to put text qualifiers around certain values. Here is a sample of the CSV file I need to output:
Telephone Directory Assignment,vid7.23,7.0.5.3,,,,
0,1,2,3,4,5,6
Name,Number,Prime Name,Privacy,Cluster Element Id/Int/Ext/Speed Call,Department,Location
"Bloggs,Joe",9~12345678,No,No, ,,
"Beam,Jim",9~12345678,No,No, ,,
"Smith,John",9~12345678,No,No, ,,
"Doe,John",9~12345678,No,No, ,,
The CSV file is then uploaded to a server where it is converted into the server's address book format and sent out to all our VoIP phones.
I can output the CSV no problem but what I cannot do is put the text qualifiers starting from the 4th row onwards. When I output the CSV, every row in the first column has "" around it! Here is my code:
Private Sub ExportGridToCSV()
Dim MyConnection As SqlConnection
Dim MyDataAdapter As SqlDataAdapter
Dim MyDataTable As New DataTable
Dim strSQL As String = _
"SELECT [phonebook_Name], [phonebook_Number], [phonebook_Primename], [phonebook_Privacy], [phonebook_Cluster],
[phonebook_Dept], [phonebook_Location] FROM [wb_TelephoneBook]"
MyConnection = New SqlConnection( _
ConfigurationManager.ConnectionStrings("plistConnectionString").ConnectionString)
MyConnection.Open()
MyDataAdapter = New SqlDataAdapter(strSQL, MyConnection)
MyDataAdapter.Fill(MyDataTable)
MyConnection.Close()
If IO.File.Exists("GridData.csv") Then IO.File.Delete("GridData.csv")
Dim sw As StreamWriter = New StreamWriter(Server.MapPath("GridData.csv"), False)
Dim iColCount As Integer = MyDataTable.Columns.Count
Dim iRowCount As Integer = MyDataTable.Rows.Count
Dim i As Integer = 0
For Each dr As DataRow In MyDataTable.Rows
i = 0
Do While i < iColCount
If (Not Convert.IsDBNull(dr(i))) Then
If i = 0 Then ' checking for first column
sw.Write("""" & dr(i).ToString() & """")
Else
sw.Write(dr(i).ToString())
End If
End If
If i < iColCount - 1 Then
sw.Write(",")
End If
i += 1
Loop
sw.Write(sw.NewLine)
Next dr
sw.Close()
End Sub
I can check to see if I am on the first column but I cannot find a way to check what row I am currently looping through. Why is there no CurrentRow property! :confused:
If anyone has an idea on how I can check the row index (so I can apply the text qualifiers from row 4 onwards) then please post them.
Thanks.
I am getting slightly frustrated with this problem now and everthing I try does not work! I have some code that selects data from a SQL table and stores it in a DataTable. I then loop through this DataTable to write out the data as CSV. The problem is that I have to put text qualifiers around certain values. Here is a sample of the CSV file I need to output:
Telephone Directory Assignment,vid7.23,7.0.5.3,,,,
0,1,2,3,4,5,6
Name,Number,Prime Name,Privacy,Cluster Element Id/Int/Ext/Speed Call,Department,Location
"Bloggs,Joe",9~12345678,No,No, ,,
"Beam,Jim",9~12345678,No,No, ,,
"Smith,John",9~12345678,No,No, ,,
"Doe,John",9~12345678,No,No, ,,
The CSV file is then uploaded to a server where it is converted into the server's address book format and sent out to all our VoIP phones.
I can output the CSV no problem but what I cannot do is put the text qualifiers starting from the 4th row onwards. When I output the CSV, every row in the first column has "" around it! Here is my code:
Private Sub ExportGridToCSV()
Dim MyConnection As SqlConnection
Dim MyDataAdapter As SqlDataAdapter
Dim MyDataTable As New DataTable
Dim strSQL As String = _
"SELECT [phonebook_Name], [phonebook_Number], [phonebook_Primename], [phonebook_Privacy], [phonebook_Cluster],
[phonebook_Dept], [phonebook_Location] FROM [wb_TelephoneBook]"
MyConnection = New SqlConnection( _
ConfigurationManager.ConnectionStrings("plistConnectionString").ConnectionString)
MyConnection.Open()
MyDataAdapter = New SqlDataAdapter(strSQL, MyConnection)
MyDataAdapter.Fill(MyDataTable)
MyConnection.Close()
If IO.File.Exists("GridData.csv") Then IO.File.Delete("GridData.csv")
Dim sw As StreamWriter = New StreamWriter(Server.MapPath("GridData.csv"), False)
Dim iColCount As Integer = MyDataTable.Columns.Count
Dim iRowCount As Integer = MyDataTable.Rows.Count
Dim i As Integer = 0
For Each dr As DataRow In MyDataTable.Rows
i = 0
Do While i < iColCount
If (Not Convert.IsDBNull(dr(i))) Then
If i = 0 Then ' checking for first column
sw.Write("""" & dr(i).ToString() & """")
Else
sw.Write(dr(i).ToString())
End If
End If
If i < iColCount - 1 Then
sw.Write(",")
End If
i += 1
Loop
sw.Write(sw.NewLine)
Next dr
sw.Close()
End Sub
I can check to see if I am on the first column but I cannot find a way to check what row I am currently looping through. Why is there no CurrentRow property! :confused:
If anyone has an idea on how I can check the row index (so I can apply the text qualifiers from row 4 onwards) then please post them.
Thanks.