English 中文(简体)
仅读CSV文档中的特殊领域。
原标题:Read only particular fields from CSV File in vb.net
  • 时间:2010-04-29 21:43:39
  •  标签:
  • vb.net

我的这部法典已读到加拿大统计局的档案。 每一行各行各行各行,各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各行各处,并将外地价值储存在阵列。

我如何从CSV档案中提取只需要的领域?

例如,我有类似CSV档案。

Type,Group,No,Sequence No,Row No,Date (newline) 0,Admin,3,345678,1,26052010 (newline) 1,Staff,5,78654,3,26052010

我只需要一栏,即号码和日期。

预先感谢任何想法。

Dim myStream As StreamReader = Nothing
      Hold the Parsed Data
    Dim strlines() As String
    Dim strline() As String
    Try
      myStream = File.OpenText(OpenFile.FileName)
      If (myStream IsNot Nothing) Then
          Hold the amount of lines already read in a  counter-variable  
        Dim placeholder As Integer = 0
        strlines = myStream.ReadToEnd().Split(Environment.NewLine)
        Do While strlines.Length <> -1   Is -1 when no data exists on the next line of the CSV file
          strline = strlines(placeholder).Split(",")
          placeholder += 1
        Loop
      End If
    Catch ex As Exception
      LogErrorException(ex)

    Finally

      If (myStream IsNot Nothing) Then
        myStream.Close()
      End If
    End Try
最佳回答

1DO NOT USEString. 页: 1

CSV数据可以包含 com,例如。

id,name
1,foo
2,"hans, bar"

此外,如上所述,你需要处理所引述的领域...... 详情见CSVInfo

(2) 核对Text FieldParser—— 它含有所有此类内容。

它将处理你可能与扼杀有关的无数不同越狱事件。 分裂

Sample from: http://msdn.microsoft.com/en-us/library/caka7e6.aspx

Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:TestFolder	est.txt")
    MyReader.TextFieldType = FileIO.FieldType.Delimited
    MyReader.SetDelimiters(",")
    Dim currentRow As String()
    While Not MyReader.EndOfData
        Try
            currentRow = MyReader.ReadFields()
            Dim currentField As String
            For Each currentField In currentRow
            MsgBox(currentField)
            Next
        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
        End Try
    End While
End Using

<代码>MyReader.Read Fields(>>部分将给你一系列的座标,从那儿,你需要使用索引等......。

PK:-

问题回答

也许不要仅仅进口选定的田地,你就应该进口一切,然后只能使用你需要的东西。





相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?