我正在尝试读取.csv文件内容,然后找到包含字母N的记录,并将包含该N的行的其余部分导出到外部.txt文件。
此时,总共有12个客户,其中7个包含我需要的字母N.我希望它们全部导出。只有找到的第一个才会被导出。
CSV文件{A列和B列}
John 345N
Andy 346K
Andrew 564B
Richard 645N
John 563N
Andy 345N
Andrew 346K
Richard 564B
John 645N
Andy 563N
Andrew 345N
Richard 346K
代码:
Dim currentRow As String()
Dim customerName(11) As String
Dim idAndProduct(11) As String
Dim FileWriter As StreamWriter
Using FileReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\Files\customers.csv")
FileReader.TextFieldType = FileIO.FieldType.Delimited
FileReader.SetDelimiters(",")
For index = 0 To 11
currentRow = FileReader.ReadFields
customerName(index) = currentRow(0)
idAndProduct(index) = currentRow(1)
Next index
For counter = 0 To 11
If Mid$(idAndProduct(counter), 4, 1) = "N" Then
FileWriter = New StreamWriter("C:\Files\export.txt")
FileWriter.WriteLine(customerName(counter) & "," & idAndProduct(counter))
FileWriter.Close()
End If
Next counter
End Using
输出(只导出一行而不是全部7行):
Andrew,345N
如何让所有7个导出?
我会感谢任何答案!
编辑要解决此问题,需要将代码从FileWriter = New StreamWriter(“C:\ Files \ export.txt”)更改为FileWriter = New StreamWriter(“C:\ Files \” export.txt“,True)以防止文件被覆盖。