开发者

After importing .csv file into textbox. Special characters shows up as "�"

I am trying to import a .csv file exported from google contacts into a textbox in VB.net. For some reason characters such as é å ä ö turns out as � when imported.

This is the code used to import the file:

        Dim ofd As New OpenFileDialog()
    ofd.CheckFileExists = True
    ofd.CheckPathExists = True
    ofd.Filter = "Text Files|*.csv" 'for multiple filters do this:
    'ofd.Filter = "Text Files, X开发者_StackOverflow中文版ML Files|*.txt;*.xml"
    If ofd.ShowDialog() = DialogResult.OK Then
        Using sr As New StreamReader(ofd.FileName)
            txtInput.Text = sr.ReadToEnd()
        End Using
    End If

How can I fix this?


You're reading the file with the wrong encoding.

Figure out what encoding it really is (probably Encoding.GetEncoding(1252)), then pass the correct Encoding instance to whatever method you're using to the StreamReader constructor.


The .CSV file was encoded with ANSII. Didn't find how to use ANSII while importing so I re-saved the file using UTF-8 so now it works. I'm sure there is a way to do it without having to do it manually like that but this will have to do for now I guess.


Private Sub cmdStore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStore.Click
        'InsertDBValues()
     Dim filename As String = "C:\gProducts.csv"
        Dim fields As String()
        Dim delimiter As String = ","
        Using parser As New TextFieldParser(filename)
            parser.SetDelimiters(delimiter)
            While Not parser.EndOfData
                ' Read in the fields for the current line
                fields = parser.ReadFields()
                ' Add code here to use data in fields variable.
                txtName.Text = fields(1).ToString ' likewise give all textbox 

            End While
        End Using

    End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜