I need to export data from VB into a csv file. I have used the following code to format the csv file.
Most of the data contains phone information, and most of the phone numbers start with 0. Excel trims the leading 0 from the phone numbers in the csv file. Is it possible to specify that all colums should be set as text rather thqan number so that the trimming does not occur?
Also, is it possible to set the width of the columns in the csv so that the user doesn't need to manually adjust the column widths before printing?
VB.NET:
Public Function Export(ByVal ds As DataSet) As String
Dim header As String
Dim body As String
Dim record As String
Dim sb As New StringBuilder
For Each col As DataColumn In ds.Tables(0).Columns
sb.Append(Helper.FormatTextForCSV(col.ColumnName))
sb.Append(",")
Next
sb.Replace(",", "", sb.Length - 1, 1)
sb.Append(vbCrLf)
For Each row As DataRow In ds.Tables(0).Rows
Dim arr() As Object = row.ItemArray()
For i As Integer = 0 To arr.Length - 1
sb.Append(Helper.FormatTextForCSV(arr(i).ToString))
sb.Append(",")
Next
sb.Replace(",", "", sb.Length - 1, 1)
sb.Append(vbCrLf)
Next
Return sb.ToString()
End Function
Most of the data contains phone information, and most of the phone numbers start with 0. Excel trims the leading 0 from the phone numbers in the csv file. Is it possible to specify that all colums should be set as text rather thqan number so that the trimming does not occur?
Also, is it possible to set the width of the columns in the csv so that the user doesn't need to manually adjust the column widths before printing?