如何在读取文件时摆脱 vb.net 中的空行

How to get rid of empty row in vb.net while reading file

下面是从具有 3 列(不断变化)的 .CSV 文件中读取数据的代码。有时,当用户打开 Excel 中的 .csv 并删除其中的一行时,该行会被读取为空行或空白行,并以逗号分隔。

当通过代码读取相同内容时,我得到“,”作为输入,它作为空行添加到我的数据表中。我怎样才能转义这个空白行?

Dim sreader As StreamReader
Dim sstring As String
Dim dt As DataTable
Dim counter as Integer
sreader = File.OpenText(Path.ToString) 'this path is path of the excel
    
While sreader.Peek <> -1

    sstring = sreader.Readline()
    If sstring <> " " then  ' how can I check here that the string does not have any content in it except for the seperating commas
    Dim str As String () = sstring.Split(",")
    
    
    Dim rowdt As DataRow
    rowdt = dt.NewRow()
    
    For i As Integer = 0 To dt.Columns.count-1
        rowdt(i) = str(i).ToString()
    Next

    dt.rows.Add(rowdt)
    End if
Counter = counter + 1
End While

我尝试了一些东西。已在回答区发帖

这是我试过的

Dim sreader As StreamReader
Dim counter as Integer
Dim sstring As String
Dim dt As DataTable
sreader = File.OpenText(Path.ToString) 

 While sreader.Peek <> -1

 sstring = sreader.Readline()

 Dim no as integer = 0

 For each str as String in sstring.Split(",")
  If str.ToString.Trim = "" then
   no = no + 1
   End If
  Next


  If no <> 3 then 
 Dim str As String () = sstring.Split(",")


 Dim rowdt As DataRow
 rowdt = dt.NewRow()

  For i As Integer = 0 To dt.Columns.count-1
rowdt(i) = str(i).ToString()
Next

 dt.rows.Add(rowdt)
 End if
 End if
counter = counter + 1
End While

读取所有行,然后只处理逗号之间有任何值的行

Dim path = "filename.txt"
Dim dt As New DataTable()
dt.Columns.AddRange(
    {
        New DataColumn("Column1"), New DataColumn("Column2"),
        New DataColumn("Column3"), New DataColumn("Column4"),
        New DataColumn("Column5"), New DataColumn("Column6"),
        New DataColumn("Column7"), New DataColumn("Column8"),
        New DataColumn("Column9"), New DataColumn("Column10")
    })

Dim sw As New Stopwatch()
sw.Start()

Dim lines = File.ReadAllLines(Path)
For Each line In lines
    Dim split = line.Split({","c}, StringSplitOptions.None)
    If split.Any(Function(s) Not String.IsNullOrWhiteSpace(s)) Then
        Dim row = dt.NewRow()
        For i As Integer = 0 To dt.Columns.Count - 1
            row(i) = split(i).ToString()
        Next
        dt.Rows.Add(row)
    End If
Next

sw.Stop()
Console.WriteLine($"Took {sw.ElapsedMilliseconds} ms")
Console.WriteLine($"Read {dt.Rows.Count()} rows")

经过测试以解决性能问题

文件内容 1024行a,b,c,d,e,f,g,h,i,j和一些,,,,,,,,,行混合在一起,包括文件的最后一行

Final 文件的第 10 行:

a,b,c,d,e,f,g,h,i,j
a,b,c,d,e,f,g,h,i,j
,,,,,,,,,
,,,,,,,,,
a,b,c,d,e,f,g,h,i,j
a,b,c,d,e,f,g,h,i,j
,,,,,,,,,
a,b,c,d,e,f,g,h,i,j
a,b,c,d,e,f,g,h,i,j
,,,,,,,,,

秒表对象显示读取所有行需要 2 毫秒。结果DataTable中正好有1024行数据。处理器跳过没有值的行

Took 2 ms
Read 1024 rows

我试过拆分和检查字符串。希望有用。

Dim sreader As StreamReader
Dim counter as Integer
Dim sstring As String
Dim dt As DataTable
sreader = File.OpenText(Path.ToString) 

While sreader.Peek <> -1

    sstring = sreader.Readline()

    Dim no as integer = 0

    For each str as String in sstring.Split(",")
        If str.ToString.Trim = "" then
            no = no + 1
        End If
    Next

    If no <> 3 then 
        Dim str As String () = sstring.Split(",")

        Dim rowdt As DataRow
        rowdt = dt.NewRow()

        For i As Integer = 0 To dt.Columns.count-1
            rowdt(i) = str(i).ToString()
        Next

        dt.rows.Add(rowdt)
    End if
    End if
    counter = counter + 1
End While