我正在尝试将从谷歌联系人导出的.csv文件导入到VB.net中的文本框中。由于某些原因,在导入时,像éasäö这样的字符会显示为�。
以下是用于导入文件的代码:
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, XML Files|*.txt;*.xml"
If ofd.ShowDialog() = DialogResult.OK Then
Using sr As New StreamReader(ofd.FileName)
txtInput.Text = sr.ReadToEnd()
End Using
End If我该如何解决这个问题呢?
发布于 2011-04-07 00:37:16
您正在使用错误的编码读取文件。
弄清楚它到底是什么编码(可能是Encoding.GetEncoding(1252)),然后将正确的Encoding实例传递给StreamReader构造函数所使用的任何方法。
发布于 2011-04-07 01:07:30
.CSV文件是用ANSII编码的。我不知道如何在导入时使用ANSII,所以我使用UTF-8重新保存了文件,所以现在它可以工作了。我确信有一种方法可以做到这一点,而不需要像这样手动完成,但我想现在必须这样做。
发布于 2013-01-24 14:49:03
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 Subhttps://stackoverflow.com/questions/5569888
复制相似问题