如何在读取文件时清除vb.net中的空行

  • 本文关键字:net vb 清除 读取 文件 vb.net
  • 更新时间 :
  • 英文 :


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

当通过代码读取相同的代码时"作为输入,作为空行添加到我的dataTable中。我该如何逃离这一空白行?

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和一些行,,,,,,,,,,包括文件的最后一行

文件的最后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
,,,,、,,,

StopWatch对象显示读取所有行需要2ms。在生成的DataTable中正好有1024行数据。处理器跳过没有值的行

耗时2毫秒
读取1024行

我尝试过拆分和检查字符串。希望它能起作用。

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

最新更新