0001 /04/01 Y-werqwerqwerwe0002 /04/02 N-vvvad0003 /04/02 Y-sfasf0004 /04/02 Y-120005 /04/03 Y-23faas0006 /04/03 N-124140007 /04/03 Y-x以上类似的文本内容
在定宽文本文件中,结尾处的字段可以具有可变宽度。 若要指定结尾处的字段具有可变宽度,请将它定义为宽度小于或等于零。
Public Class Form1Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickOpenFileDialog1.Filter = "Text Files(*.TXT)|*.TXT"If OpenFileDialog1.ShowDialog = DialogResult.OK ThenDim fileName As String = OpenFileDialog1.FileNameUsing Reader As New Microsoft.VisualBasic.FileIO.TextFieldParser(fileName)Reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.FixedWidthReader.SetFieldWidths(5, 11, -1)Dim currentRow As String()While Not Reader.EndOfDataTrycurrentRow = Reader.ReadFields()Dim currentField As StringFor Each currentField In currentRowTextBox1.Text &= vbCrLf & currentFieldNextCatch ex As Microsoft.VisualBasic.FileIO.MalformedLineExceptionMsgBox("Line " & ex.Message & "is not valid and will be skipped.")End TryEnd WhileEnd UsingEnd IfEnd SubEnd Class