如何将一个巨大的文本文件读取到 ObservableCollection
How to read a huge txt file to a ObservableCollection
我有一个 excel 文件,其中包含超过 200,000 行。如果我使用 OleDbConnection 将其读入数据表或 ObservableCollection,然后将其显示在数据网格中,则需要一分钟多的时间。我将 xlsx 更改为制表符分隔的 txt。我发现将整个 txt 文件放入数据表中非常快。不到10s。如果我将整个 txt 直接放入 ObservableCollection,我想知道该怎么做。它不能逐行完成,因为它会再次花费一分钟多的时间。
'Define Product property
Public Class Product
Public Property Model As String
Public Property Opt As String
Public Property Description As String
Public Property Price As String
End Class
'define Products as ObservableCollection
Dim Products As New ObservableCollection(Of Product)()
'Read txt file to datatable
Private Sub LoadBound(ByVal fName As String)
Dim textLine As String = String.Empty
Dim splitLine As String()
CSVdata.Columns.AddRange({New DataColumn("Model"),
New DataColumn("Opt"),
New DataColumn("Description"),
New DataColumn("Price")})
CSVdata.Rows.Clear()
If System.IO.File.Exists(fName) Then
Dim objReader As System.IO.StreamReader = New System.IO.StreamReader(fName)
Dim contents = objReader.ReadToEnd()
Dim strReader = New System.IO.StringReader(contents)
Do
textLine = strReader.ReadLine()
If textLine.Contains("""") Then
textLine = textLine.Replace("""", "")
End If
If textLine <> String.Empty Then
splitLine = textLine.Split(vbTab)
If splitLine(0) <> String.Empty OrElse splitLine(1) <> String.Empty Then
CSVdata.Rows.Add(splitLine)
End If
End If
Loop While strReader.Peek() <> -1
End If
End Sub
在线评论和解释。
Public Class Product
Public Property Model As String
Public Property Opt As String
Public Property Description As String
Public Property Price As String
'Add a parameterized constructor to your class to make coding easier
Public Sub New(Mdl As String, O As String, Desc As String, P As String)
Model = Mdl
Opt = O
Description = Desc
Price = P
End Sub
End Class
Dim Products As ObservableCollection(Of Product)
Private Sub LoadBound(ByVal fName As String)
'Build a List(Of Product) from the text file
Dim lstProducts As New List(Of Product)
Dim lines = File.ReadAllLines(fName)
For Each line In lines
Dim Props = line.Split(CChar(vbTab))
Dim p As New Product(Props(0), Props(1), Props(2), Props(3))
lstProducts.Add(p)
Next
'The constructor of an ObservableCollection can take a List(Of T)
Products = New ObservableCollection(Of Product)(lstProducts)
End Sub
编辑
Public Class Product
Public Property Model As String
Public Property Opt As String
Public Property Description As String
Private strPrice As String
Public Property Price As Double
'Add a parameterized constructor to your class to make coding easier
Public Sub New(Mdl As String, O As String, Desc As String, P As String)
Model = Mdl
Opt = O
Description = Desc
Dim ParsedDouble As Double
Double.TryParse(P, ParsedDouble)
Price = ParsedDouble
End Sub
End Class
更改价格类型
更改 属性
的类型
调整 Sub New 以从字符串中获取双精度值。如果解析失败,价格将为 0。
如果您真的想要一个空值(vb 中没有任何内容),请参阅 https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/nullable-value-types
我有一个 excel 文件,其中包含超过 200,000 行。如果我使用 OleDbConnection 将其读入数据表或 ObservableCollection,然后将其显示在数据网格中,则需要一分钟多的时间。我将 xlsx 更改为制表符分隔的 txt。我发现将整个 txt 文件放入数据表中非常快。不到10s。如果我将整个 txt 直接放入 ObservableCollection,我想知道该怎么做。它不能逐行完成,因为它会再次花费一分钟多的时间。
'Define Product property
Public Class Product
Public Property Model As String
Public Property Opt As String
Public Property Description As String
Public Property Price As String
End Class
'define Products as ObservableCollection
Dim Products As New ObservableCollection(Of Product)()
'Read txt file to datatable
Private Sub LoadBound(ByVal fName As String)
Dim textLine As String = String.Empty
Dim splitLine As String()
CSVdata.Columns.AddRange({New DataColumn("Model"),
New DataColumn("Opt"),
New DataColumn("Description"),
New DataColumn("Price")})
CSVdata.Rows.Clear()
If System.IO.File.Exists(fName) Then
Dim objReader As System.IO.StreamReader = New System.IO.StreamReader(fName)
Dim contents = objReader.ReadToEnd()
Dim strReader = New System.IO.StringReader(contents)
Do
textLine = strReader.ReadLine()
If textLine.Contains("""") Then
textLine = textLine.Replace("""", "")
End If
If textLine <> String.Empty Then
splitLine = textLine.Split(vbTab)
If splitLine(0) <> String.Empty OrElse splitLine(1) <> String.Empty Then
CSVdata.Rows.Add(splitLine)
End If
End If
Loop While strReader.Peek() <> -1
End If
End Sub
在线评论和解释。
Public Class Product
Public Property Model As String
Public Property Opt As String
Public Property Description As String
Public Property Price As String
'Add a parameterized constructor to your class to make coding easier
Public Sub New(Mdl As String, O As String, Desc As String, P As String)
Model = Mdl
Opt = O
Description = Desc
Price = P
End Sub
End Class
Dim Products As ObservableCollection(Of Product)
Private Sub LoadBound(ByVal fName As String)
'Build a List(Of Product) from the text file
Dim lstProducts As New List(Of Product)
Dim lines = File.ReadAllLines(fName)
For Each line In lines
Dim Props = line.Split(CChar(vbTab))
Dim p As New Product(Props(0), Props(1), Props(2), Props(3))
lstProducts.Add(p)
Next
'The constructor of an ObservableCollection can take a List(Of T)
Products = New ObservableCollection(Of Product)(lstProducts)
End Sub
编辑
Public Class Product
Public Property Model As String
Public Property Opt As String
Public Property Description As String
Private strPrice As String
Public Property Price As Double
'Add a parameterized constructor to your class to make coding easier
Public Sub New(Mdl As String, O As String, Desc As String, P As String)
Model = Mdl
Opt = O
Description = Desc
Dim ParsedDouble As Double
Double.TryParse(P, ParsedDouble)
Price = ParsedDouble
End Sub
End Class
更改价格类型
更改 属性
的类型调整 Sub New 以从字符串中获取双精度值。如果解析失败,价格将为 0。
如果您真的想要一个空值(vb 中没有任何内容),请参阅 https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/nullable-value-types