Nathan 0

Nathan 0

  • NA
  • 1
  • 0

Comma and Tab seperator help

Jan 4 2007 12:51 AM
Hello. I'm working on a project that has pre-existing code for importing and exporting certain data to a CSV file. In this case, the data being imported and exported could contain a comma, so I used a TAB seperator. Also, the data being imported and exported here has no use outside the project so the fact that Excel 2003 doesn't like to open TAB seperated CSV files wasn't an issue.

Now, i'm adding another import and export section for use with other data, and this data will not feature any commas. However, it needs to be easily usable in Excel 2003 which only seems to natively like CSV files with commas. I changed the Export code I had before to export using commas instead of TABs without issue, but the import code is having trouble adapting to importing CSV data with commas.

I thought changing Chr(9) to Chr(44) would do it, but it hasn't. It is loading the file fine, but it is only loading the 1st field in every record, as trying to reference the 2nd+ field generates an Out of Bounds error for the array. Anyway, here is my importing code (NOTE: The 1st line of the CSV is a version number line and the code here is designed to ignore it. This part works fine in the other import code I have working so that isn't the issue from what I can see):



OpenFileDialog3.Filter = "CSV (*.CSV)|*.csv"
If OpenFileDialog3.ShowDialog() = DialogResult.OK Then
Dim i, j As Integer
Dim srCSV As New System.IO.StreamReader(OpenFileDialog3.FileName)
Dim strRecords As String(,)
Dim strCsvfile As String

strCsvfile = srCSV.ReadToEnd()
srCSV.Close()

Dim chrReturn As Char = Chr(13)
Dim strRecordsAsWhole As String() = strCsvfile.Split(chrReturn)

Dim intNumberofRecords As Integer = strRecordsAsWhole.GetUpperBound(0) - 2
Dim intNumberofFields As Integer = CountFields(strRecordsAsWhole(1))

ReDim strRecords(intNumberofRecords, intNumberofFields)
Dim strFields As String()

For i = 0 To intNumberofRecords
strFields = strRecordsAsWhole(i + 1).Split(Chr(44))

For j = 0 To intNumberofFields
strRecords(i, j) = strFields(j).Trim()
Next
Next

Dim counter As Integer

TextBox176.Text = intNumberofFields

While counter <= intNumberofRecords

counter = ((counter) + 1)
End While

End If

I have cut down on the unneccesary stuff - obviously in the while loop i'm using counter to gather every field from every record which is done via x = strRecords(counter,0) for the 1st field and strRecords(counter,1) for the 2nd field etc.

You'll notice I have the line 'TextBox176.Text = intNumberofFields' - I created a dummy textbox to check what value IntNumberofFields was obtaining after building the project and it is generating '0' at the moment (there should be 129 fields). I added this to the import code that works with TAB seperators and it was reporting the correct number. So obviously for some reason, during the shift to commas, intNumberofFields is not recording the right number of fields. This would explain why the code only imports the first field from every record when using commas as well.

So any help as to why this import CSV data code that works with TAB's but not commas is much apprediated, thanks!