根据您对数据结构的解释:
Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions
Module Module1
Class Cell
Property ColumnName As String
Property Value As String
' To help with debugging/general usage
Public Overrides Function ToString() As String
Return String.Format("Col: {0} Val: {1}", ColumnName, Value)
End Function
End Class
Dim table As New List(Of List(Of Cell))
Sub Main()
Dim src As String = "C:\temp\sampledata.txt"
Dim dest = "C:\temp\sampledata.csv"
Dim colNames As New List(Of String)
' This regex will look for zero or more characters ".*" surrounded by braces "\{ \}" and
' collect the zero or more characters in a group "( )". The "?" makes it non-greedy.
' The second capture group "( )" gets all the characters up to but not including
' the next "\{" (if it is present).
Dim cellSelector = New Regex("\{(.*?)\}([^\{]*)")
' Read in the cells and record the column names.
Using inFile = New StreamReader(src)
While Not inFile.EndOfStream
Dim line = inFile.ReadLine
Dim rowContent As New List(Of Cell)
For Each m As Match In cellSelector.Matches(line)
rowContent.Add(New Cell With {.ColumnName = m.Groups(1).Value, .Value = m.Groups(2).Value})
If Not colNames.Contains(m.Groups(1).Value) Then
colNames.Add(m.Groups(1).Value)
End If
Next
table.Add(rowContent.OrderBy(Function(c) c.ColumnName).ToList)
End While
End Using
colNames.Sort()
' add the header row of the column names
Dim sb As New StringBuilder(String.Join(",", colNames) & vbCrLf)
' output the data in csv format
For Each r In table
Dim col = 0
Dim cellNo = 0
While cellNo < r.Count AndAlso col < colNames.Count
' If this row has a cell with the appropriate column name then
' add the value to the output.
If r(cellNo).ColumnName = colNames(col) Then
sb.Append(r(cellNo).Value)
cellNo += 1
End If
' add a separator if is not the last item in the row
If col < colNames.Count - 1 Then
sb.Append(","c)
End If
col += 1
End While
sb.AppendLine()
Next
File.WriteAllText(dest, sb.ToString)
End Sub
End Module
根据您的样本数据,输出是
1000,1200,1500,1600,3000,4000,5000,6000
xxx,xxx,,,xxxxxx,,,
xx,,xxxxxx,,,xx,,,
xxxx,,,xxx,xxx,,,,
我注意到最后一列都没有数据。这只是复制粘贴错误还是故意的?
编辑:我使用Option Infer On,这就是缺少某些类型声明的原因。