importing a CSV file into my app...get wierd characters

ghardy

Member
Joined
Mar 18, 2010
Messages
20
Programming Experience
10+
i am importing a csv file into my app. this is to let the users pull in info they have in excel.

somehow, they have slanted apostrophes, and not tick marks. so when i open the file stream and do the import, it pulls in those characters as blank boxes (unknown char).

is there a way to compensate for this?
 
I would suggest that you use a TextFieldParser to read a CSV file:
VB.NET:
Using reader As New FileIO.TextFieldParser(filePath)
    reader.Delimiters = New String() {","}

    Do Until reader.EndOfData
        'Read the next line.
        Dim fields As String() = reader.ReadFields()

        'Process the data in whatever way is appropriate.
        MessageBox.Show(String.Join(Environment.NewLine, fields))
    Loop
End Using
Now, to solve your problem, you need to trim the ` character from each field value in each row. For that, define a method that does it for a single value:
VB.NET:
Private Function TrimQuotes(ByVal text As String) As String
    Return text.Trim("`"c)
End Function
and now make use of that function in your loop:
VB.NET:
Using reader As New FileIO.TextFieldParser(filePath)
    reader.Delimiters = New String() {","}

    Do Until reader.EndOfData
        'Read the next line.
        Dim fields As String() = reader.ReadFields()

[COLOR="red"]        'Remove enclosing quotes from each field value.
        fields = Array.ConvertAll(fields, AddressOf TrimQuotes)[/COLOR]

        'Process the data in whatever way is appropriate.
        MessageBox.Show(String.Join(Environment.NewLine, fields))
    Loop
End Using
The Array.ConvertAll method will take an existing array and return a new array, of the same length, where each element in the new array is the result of calling the specified function and passing the corresponding element of the old array.
 
Back
Top