简体   繁体   English

VS2010中的VB.NET-尝试读取可能包含货币符号的csv

[英]VB.NET in VS2010 - Trying to read in a csv that can contain currency signs

I'm working on an application that will read in delimited files and display the first few rows on a datagridview, it will then use certain elements from these rows to do a SQL query (postcode lookup). 我正在开发一个应用程序,该应用程序将读取分隔的文件并在datagridview上显示前几行,然后它将使用这些行中的某些元素进行SQL查询(邮政编码查找)。 I had it all working fine until we got some data where one of the column values contained a £, when it displays in the datagrid it looks like a diamond with a ? 我一直运转良好,直到我们得到一些数据,其中一个列值中包含一个£,当它显示在数据网格中时,它看起来像是带有?的菱形。 in it but even worse when it writes the file back out (with the SQL result added) it comes out as � 在其中,但更糟的是,当它回写文件(添加了SQL结果)时,它的输出为�

I thought I probably needed to specify the encoding but I was using the following which has no encoding setting that I can find: 我以为我可能需要指定编码,但是我使用的是以下没有找到的编码设置:

    Sub ParseFile(inputfile)
    Dim dataFile = My.Computer.FileSystem.OpenTextFieldParser(inputFile)
    Using dataFile
        dataFile.TextFieldType = FileIO.FieldType.Delimited
        If DelimiterComboBox.SelectedItem = ".csv" Then
            dataFile.SetDelimiters(",")
        ElseIf DelimiterComboBox.SelectedItem = ".txt" Then
            dataFile.SetDelimiters(vbTab)
        End If
        Dim currentRow As String()
        currentRow = dataFile.ReadFields()

I tried changing that to use a TextFieldReader which would allow the Encoding to be specified but I can't pass the filename in as a variable then as I get conversion errors. 我尝试将其更改为使用TextFieldReader,这将允许指定Encoding,但是当出现转换错误时,我无法将文件名作为变量传递。 I then tried defining a variable as a stream of the filename but then I get: Unable to cast object of type 'System.String' to type 'System.IO.Stream'. 然后,我尝试将变量定义为文件名流,但随后得到:无法将类型为“ System.String”的对象转换为类型为“ System.IO.Stream”。

I'm thinking that I need to construct a stream from the filename somehow and feed that into the "dataFile" object so I can then control (or even detect) the encoding. 我在想,我需要以某种方式从文件名构造一个流,并将其输入到“ dataFile”对象中,以便随后可以控制(甚至检测)编码。

I should point out that this app needs to work with various delimited files (.csv, tab and pipe being the most common). 我应该指出,此应用需要使用各种定界文件(.csv,制表符和管道是最常见的)。

Try 尝试

Using dataFile As TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser(New System.IO.StreamReader(inputFile, encoding))
...
End Using

See http://msdn.microsoft.com/en-us/library/system.text.encoding(v=vs.90).aspx for more information on things you can specify for the encoding parameter. 有关可为encoding参数指定的内容的更多信息,请参见http://msdn.microsoft.com/zh-cn/library/system.text.encoding(v=vs.90).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM