我正在尝试通读文本文件,但后来只显示某些字段返回到我的DataGridView,而不是整个文件。我将分隔符设置为" +"然后想要显示字段开头的文件,例如EQD。 我的文件看起来像这样,其中每一行都是由" ' ":
UNB+UNOA:1+++160804:0850+1+++++1'
UNH+402+BAPLIE:1:911:UN:SMDG15'
BGM++580691+9'
DTM+137:2016080408 50:301'
TDT+20+6217++++MACS:172:166'
LOC+5+NAWVB:139:6'
LOC+61+ZACPT:139:6'
DTM+178:1608020718:201'
DTM+133:1607030700:201'
DTM+132:160702:101'
LOC+147+0121282::5'
MEA+WT++KGM:4200'
LOC+6+PTLEI'
LOC+12+ZADUR'
RFF+BM:1'
****EQD+CN+CXRU1123659+45R1+++4'****
NAD+CA+MACS:172:20'
UNT+2339+402'
UNZ+1+1'
我目前的代码是:
Imports System.IO
Imports System.Text.RegularExpressions
Imports System
Imports System.Collections
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sourceFilename1 As String
'Dim finalFile As List(Of String) = New List(Of String)
'Dim origFileData() As String = File.ReadAllText(sourceFilename1).Replace(vbCr, "").Replace(vbLf, "").Split({"'"}, StringSplitOptions.RemoveEmptyEntries)
OpenFileDialog1.Filter = "BAPLIE Files (*.*;*.*)|*.*;*.*"
If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
sourceFilename1 = OpenFileDialog1.FileName
End If
Dim TextFieldParser1 As New Microsoft.VisualBasic.FileIO.TextFieldParser(sourceFilename1)
TextFieldParser1.Delimiters = New String() {"'"}
TextFieldParser1.ReadLine.StartsWith("EQD+CN+")
'If sourceFilename1.StartsWith("EQD+CN+") Then
' numberString = sourceFilename1.Substring(sourceFilename1.IndexOf("CN+"), 14)
' txtLastName.Text = numberString.Substring(3)
'End If
While Not TextFieldParser1.EndOfData
Dim Row1 As String() = TextFieldParser1.ReadFields()
If DataGridView1.Columns.Count = 0 AndAlso Row1.Count > 0 Then
Dim i As Integer
For i = 0 To Row1.Count - 1
DataGridView1.Columns.Add("Column" & i + 1, "Column" & i + 1)
Next
End If
DataGridView1.Rows.Add(Row1)
End While
End Sub
End Class
我没有编程经验,也没有做宠物项目。
答案 0 :(得分:0)
我重写了我的代码。我得到我的文件只在一行显示正确的信息,但现在它只在我的数据网格中显示一行,如果我运行我的程序?
这是我的新代码:
导入System.IO Imports System.Text.RegularExpressions 进口系统 Imports System.Collections
Public Class Form1 ' ------这很好 Private Sub btnRun_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnRun.Click Dim sourceFilename2 As String =" *。edi | * .TXT"
If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
sourceFilename2 = OpenFileDialog1.FileName
End If
Dim strm As IO.Stream = IO.File.OpenRead(sourceFilename2)
Dim sr As New IO.StreamReader(strm)
Dim sw = System.Diagnostics.Stopwatch.StartNew()
Dim line1 As String = ""
Dim trimchars() As Char = {"+", "'"}
grdMydata.Columns.Add("Column1", "POD") ' ------------------------- INDEX 0
grdMydata.Columns.Add("Column2", "BAY") ' ------------------------- INDEX 1
grdMydata.Columns.Add("Column3", "SLOT") ' ------------------------ INDEX 2
grdMydata.Columns.Add("Column4", "VGM WEIGHT") ' ------------------ INDEX 3
grdMydata.Columns.Add("Column5", "WT WEIGHT") ' ------------------- INDEX 4
grdMydata.Columns.Add("Column6", "CONTAINER ID") ' ---------------- INDEX 5
grdMydata.Columns.Add("Column7", "ISO CODE") ' -------------------- INDEX 6
grdMydata.Columns.Add("Column8", "FULL/EMPTY") ' ------------------ INDEX 7
grdMydata.Columns.Add("Column9", "CARRIER") ' --------------------- INDEX 8
Do While sr.Peek() <> -1
Dim Newline As String
Newline = System.Environment.NewLine
line1 = sr.ReadLine()
'-----------------The file info in the text box
'txtTopRows.Text = UNH+402+ & BAPLIE:1:911:UN:SMDG15' BAPLIE VERSION
If line1.Contains("SMDG15") Then
txtTopRows.Text = "BAPLIE ver 1.5 "
End If
If line1.Contains("SMDG20") Then
txtTopRows.Text = "BAPLIE ver 2.0 "
End If
If line1.Contains("SMDG22") Then
txtTopRows.Text = "BAPLIE ver 2.0 "
End If
'DTM+137:2016080408 50:301'
If line1.Contains("DTM+137") Then
txtTopRows.Text = txtTopRows.Text & Newline & "File Date : " & (line1.Substring(8, 10))
End If
'Port of load here
If line1.Contains("LOC+5") Then
txtTopRows.Text = txtTopRows.Text & Newline & "Port of Load : " & (line1.Substring(6, 5))
End If
'Port of Discharge here
If line1.Contains("LOC+61") Then
txtTopRows.Text = txtTopRows.Text & Newline & "Port of Disharge : " & (line1.Substring(7, 5))
End If
'-----------------------------------------------------------------------------------------
Dim row1 As Integer = Me.grdMydata.Rows.Add()
'grdMydata.Rows.Clear()
'''''Code to get all data on ONE row
row1 = grdMydata.CurrentCell.RowIndex
If line1.TrimStart(trimchars).StartsWith("LOC+147") Then
grdMydata.Rows(row1).Cells(1).Value = (line1.Substring(8, 3))
grdMydata.Rows(row1).Cells(2).Value = (line1.Substring(8, 7))
End If
If line1.TrimStart(trimchars).StartsWith("MEA+VGM") Then
grdMydata.Rows(row1).Cells(3).Value = (line1.Substring(13, 5))
End If
If line1.TrimStart(trimchars).StartsWith("MEA+WT") Then
grdMydata.Rows(row1).Cells(4).Value = (line1.Substring(12, 5))
End If
If line1.TrimStart(trimchars).StartsWith("EQD") Then
grdMydata.Rows(row1).Cells(5).Value = (line1.Substring(7, 11))
grdMydata.Rows(row1).Cells(6).Value = (line1.Substring(19, 4))
End If
If line1.TrimStart(trimchars).StartsWith("NAD+CA") Then
grdMydata.Rows.Item(row1).Cells(8).Value = (line1.Substring(7, 3))
End If
Dim blank As Boolean = True
For Each _row As DataGridViewRow In grdMydata.Rows
blank = True
For i As Integer = 0 To _row.Cells.Count - 1
If _row.Cells(i).Value IsNot Nothing AndAlso _row.Cells(i).Value <> "" Then
blank = False
Exit For
End If
Next
If blank Then
If Not _row.IsNewRow Then
grdMydata.Rows.Remove(_row)
End If
End If
Next
Loop
sr.Close()
sourceFilename2 = Nothing
sw.Stop()
txtDisplay1.Text = String.Format("File loaded in: {0} miliseconds" _
& vbCrLf & "Number of Containers: {1}", sw.ElapsedMilliseconds, grdMydata.Rows.Count)
'MsgBox ("Hai" & vbCrLf & "Welcome")
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Dim sourceFilename1 As String
Dim sw = System.Diagnostics.Stopwatch.StartNew()
sourceFilename1 = Nothing
sw.Stop()
Me.Close()
End Sub
结束班
答案 1 :(得分:0)
如果文件中没有任何值包含+
或'
,那么您可以使用Split
函数:
Dim text = File.ReadAllText(sourceFilename1)
Dim lines = Split(text, "'")
For Each line in lines
Dim values = Split(line, "+")
DataGridView1.Rows.Add(values)
Next