在VB.NET中读取CSV文件时如何忽略换行符?

时间:2020-05-22 00:33:50

标签: vb.net csv arraylist

我在VB.NET中编写了一个实用程序,该实用程序读取输入的CSV文件,进行一些处理(特别是它忽略了输入文件的前5行,并将其替换为保存在另一个文件中的标题行),并从将输入文件转换为新的输出CSV文件。

当输入数据在CSV的一列值中包含换行符时,我的程序就会失败。

当我将CSV数据行中的换行符加载到字符串数组中时,我想忽略它。

这是我的代码(嵌入在表单中)

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim incsvPath = strFileName
    Dim outcsvPath = fi.DirectoryName & "\" & outfilename
    Dim headerPath = fi.DirectoryName & "\ACTIVITY_HISTORY_HEADER.csv"


    Dim fileP As String = incsvPath
    Dim fileheader As String = headerPath
    Dim CSVheaderIn As New ArrayList
    Dim CSVlinesIn As New ArrayList
    Dim CSVout As New List(Of String)

    CSVheaderIn.AddRange(IO.File.ReadAllLines(fileheader))
    CSVlinesIn.AddRange(IO.File.ReadAllLines(fileP))

    messageTB.AppendText(vbCrLf & vbCrLf)

    For Each line As String In CSVheaderIn
        Dim nameANDnumber As String() = line.Split(",")
        messageTB.AppendText("csv file header row = " & line & vbCrLf & vbCrLf & "csv file contents follow ..." & vbCrLf)
        CSVout.Add(line)
    Next

    Dim mySubAL As ArrayList = CSVlinesIn.GetRange(5, CSVlinesIn.Count - 5)

    For Each line As String In mySubAL 'CSVlinesIn
        messageTB.AppendText(line & vbCrLf)
        CSVout.Add(line)
    Next

    IO.File.WriteAllLines(outcsvPath, CSVout.ToArray)

End Sub

1 个答案:

答案 0 :(得分:0)

实际上这是相当艰巨的工作;使用一个知道如何使用数据中的换行符来读取和写入CSV的库比使用自己的库要容易得多-不是说不能,但是这是一个已经发明的轮子,为什么要再次这样做呢? / p>

我用过Steve Hansen's Csv-在解决方案资源管理器中右键单击您的项目,选择“管理Nuget程序包”,单击“浏览”,搜索 csv ,安装正确的

Imports System.Text
Imports Csv
Imports System.IO

Module Module1

    Sub Main(args As String())

        'open the headers file
        Using hIn = File.OpenText("C:\temp\h.csv")
            'setup instruction to the csv reader with headersabsent flag so we can get the first line as data
            Dim hOptions = New CsvOptions With {.HeaderMode = HeaderMode.HeaderAbsent}
            'take the first line into an array - these are our headers
            Dim headers = CsvReader.Read(hIn, hOptions)(0).Values

            'open the data file, 
            Using fIn = File.OpenText("C:\temp\a.csv")
                'setup instruction for the reader to skip 5 rows, treat first row as data, and allow newlines in quoted fields
                Dim fOptions = New CsvOptions With {.RowsToSkip = 5, .HeaderMode = HeaderMode.HeaderAbsent, .AllowNewLineInEnclosedFieldValues = True}

                Using fOut = File.CreateText("C:\temp\a_out.csv")
                    'convert the ICsvLine rows in the reader to rows of String() that the writer will accept, and write them under the headers
                    CsvWriter.Write(fOut, headers, CsvReader.Read(fIn, fOptions).Select(Function(line) line.Values))
                End Using
            End Using
        End Using

    End Sub

End Module

您不必使用此库来读取标题;您可以file.ReadText().ReadLine().Split(","c)

如果要对元素执行每行处理,请执行以下操作:


    CsvWriter.Write(fOut, headers, CsvReader.Read(fIn, fOptions).Select(Function(line) ProcessLine(line.Values)))


    ...

    Function ProcessLine(input As String()) As String()

      'Note: If(input(8), "") returns input(8) unless it is nothing in which case "" is returned instead
      If If(input(8), "").Length > 10 Then input(8) = input(8).Remove(10) 'Trim if over 10
      If If(input(14), "").Length > 10 Then input(14) = input(14).Remove(10) 

      Return input 'Always return

    End Function