簡體   English   中英

如何在讀取文件時擺脫 vb.net 中的空行

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

下面是從.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

我不想添加額外的 for 循環來檢查字符串中的內容,因為它會妨礙性能。

讀取所有行,然后僅處理逗號之間有任何值的行

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
,,,,,,,,,

秒表 object 顯示讀取所有行需要 2 毫秒。 結果 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM