2017-03-02 81 views
0

我期待能夠通過管道(「|」)分隔的文本文件,控制我讀的行和列。指定水平和垂直運動

我以前在MS Access VBA中已經實現了這個功能,並且目前已經開始在VB.NET中開展工作,但是正在努力完成這一任務。

在Access,VBA允許作爲劃定我作爲一個記錄(Dim rs As DAO.Recordset),預先定義的鏈接的文本文件,並通過行(rs.MoveNextrs.MovePrevious),而選擇字段移動/列我需要(rs!Field1)。

在VB.NET中是否有任何等價物?到目前爲止,我只能夠找到從MSDN以下,但不知道如何操作它:

Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(FName) 

     MyReader.TextFieldType = FileIO.FieldType.Delimited 
     MyReader.SetDelimiters("|") 
     Dim currentrow As String() 

     While Not MyReader.EndOfData 

      currentrow = MyReader.ReadFields() 
      Dim currentfield As String 
      For Each currentfield In currentrow 
       MsgBox(currentfield) 
      Next 

     End While 

    End Using 

我希望能夠給某些字段存儲變量被寫入後爲.csv文件上。

回答

1

對於任何人,這可能幫助,我設法與以下兩個網站的幫助,以解決此問題:

Loop Through Files in Directory

Delimit Data

使用這些我創建了下面的按鈕,點擊子和伴隨功能:

Private Sub Button_Run_Click(sender As Object, e As EventArgs) Handles Button_Run.Click 

    ' Make a reference to a directory 
    Dim strDir As String = ("C:\") 
    Dim Dir As New System.IO.DirectoryInfo(strDir) 

    ' Get a reference to each file in that directory 
    Dim fiArr As System.IO.FileInfo() = Dir.GetFiles() 

    Dim fri As System.IO.FileInfo 

    'For each file call the formatter to write to a text file 
    'Then delete each file after writing it 
    For Each fri In fiArr 
     Call Formatter(strDir & fri.Name) 
     My.Computer.FileSystem.DeleteFile(strDir & fri.Name) 
    Next fri 

End Sub 

然後我用分隔符分隔每行,其中每個f可以獲得(使用values(0)定義的變量)和修改(使用它的索引)。控制線條就像通過更改i整數來創建使用或修改現有變量的新變量一樣簡單。 E.g:

Dim Nvalues() As String = lines(i + 1).Split("|") 

使用這個我創建了以下功能:

Public Sub Formatter(ByVal FName As String) 

    'Standard start defining stream reader to loop 
    'through and pull data from text files 
    'FName is provided from the form button sub 
    Dim StrReader As New System.IO.StreamReader(FName) 

    Dim text As String = StrReader.ReadToEnd 

    StrReader.Close() 

    Dim lines() As String = text.Split(vbLf) 

    'Define all variables that may need to be stored before writing 
    Dim x1, x2, x3, x4, x5, x6, x7, x8, x9, x0 As String 

    'Define where the data will be written to (line by line) 
    Dim FPath As String = "C:\Sample.txt" 
    If Not System.IO.File.Exists(FPath) Then 
     System.IO.File.Create(FPath).Dispose() 
    End If 
    Dim objwriter As System.IO.StreamWriter 

    'Set loop to go through all data from beginning to end using i as line index 
    Dim i As Double 
    For i = 0 To lines.Length - 1 

     'Split each line by pipes (|) allowing "values" to be indexed to pull by column 
     Dim values() As String = lines(i).Split("|") 

     'Set 1st column in row as switch item 
     Select Case values(0) 

      'If 1st column in row is 001, store required data in variables 
      'specified using the index of the value to give accurate column 
      Case "001" 
       x1 = values(1) 
       x2 = values(2) 
       x3 = values(3) 
       x3 = values(4) 

       'Repeat same as 001 but for 002 
      Case "002" 
       x4 = values(1) 
       x5 = values(2) 

       'Repeat same as 001 but for 003 
      Case "003" 
       x6 = values(1) 
       x7 = values(2) 

       'Write to file including fields 1 & 3 from 004 group 
      Case "004" 

       objwriter = System.IO.File.AppendText(FPath) 
       objwriter.WriteLine(x1 & "," & x2 & "," & x3 & "," & x4 _ 
        & "," & x5 & "," & x6 & "," & x7 & "," _ 
        & Nvalues(1) & "," & Nvalues(2)) 
       objwriter.Close() 

      Case Else 

     End Select 

     Application.DoEvents() 

    Next 

End Sub