在VB.NET中读取.csv文件中单元格中的特定值
我的目标是在.csv文件中查找单元格的特定值。 (最好不要使用任何额外的参考包) 例如,如果我的.csv文件如下所示:在VB.NET中读取.csv文件中单元格中的特定值,.net,vb.net,csv,.net,Vb.net,Csv,我的目标是在.csv文件中查找单元格的特定值。 (最好不要使用任何额外的参考包) 例如,如果我的.csv文件如下所示: A, B, C, D E, F, G, H I, J, K, L M, N, O, P 然后我想返回第2行和第3列上特定单元格的值,即G 我尝试过以下代码: Dim sData() As String csvPath = appPath & "/sample.csv" Using sr As New St
A, B, C, D
E, F, G, H
I, J, K, L
M, N, O, P
然后我想返回第2行和第3列上特定单元格的值,即G
我尝试过以下代码:
Dim sData() As String
csvPath = appPath & "/sample.csv"
Using sr As New StreamReader(csvPath)
Dim counter As Integer = 0
While Not sr.EndOfStream
sData = sr.ReadLine().Split(","c)
If counter > 2 Then
Dim Cell1 As String = sData(0).Trim()
MessageBox.Show(Cell1)
TextBox1.Text = Curdate
TextBox2.Text = OrderId
End If
counter += 1
End While
End Using
运行此代码时,会弹出一个messagebox,列出.csv文件的每一行。文本框的作用是显示csv文件中的最后一行
在给定行数和列数的情况下,我如何使其仅返回.csv文件中一个单元格的特定值?如果csv文件不是很大,最简单的方法是加载内存列表中的所有内容,然后提取所需信息
Function GetValue(row As Integer, col As Integer) As String
Dim csvPath = "/sample.csv"
Dim result As String = String.Empty
Dim lines = File.ReadAllLines(csvPath)
If row < lines.Length Then
Dim cols = lines(row).Split(","c)
If col < cols.Length Then
result = cols(col)
End If
End If
Return result
End Function
函数GetValue(行为整数,列为整数)为字符串
Dim csvPath=“/sample.csv”
Dim结果为String=String.Empty
Dim lines=File.ReadAllLines(csvPath)
如果行<行长度,则
Dim cols=行(行)。拆分(“,”c)
如果col
但是要注意两件事
如果您的CSV文件不是很大,最简单的方法是加载内存列表中的所有内容,然后提取所需信息
Function GetValue(row As Integer, col As Integer) As String
Dim csvPath = "/sample.csv"
Dim result As String = String.Empty
Dim lines = File.ReadAllLines(csvPath)
If row < lines.Length Then
Dim cols = lines(row).Split(","c)
If col < cols.Length Then
result = cols(col)
End If
End If
Return result
End Function
函数GetValue(行为整数,列为整数)为字符串
Dim csvPath=“/sample.csv”
Dim结果为String=String.Empty
Dim lines=File.ReadAllLines(csvPath)
如果行<行长度,则
Dim cols=行(行)。拆分(“,”c)
如果col
但是要注意两件事
下面是一个将该文件加载到内存并存储以供重复访问的示例:
Private data As New List(Of String())
Private Sub LoadData() ' call it ONCE (maybe from the Load() event?)
data.Clear()
csvPath = appPath & "/sample.csv"
Using sr As New StreamReader(csvPath)
While Not sr.EndOfStream
data.Add(sr.ReadLine().Split(","c))
End While
End Using
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' these are one based values
Dim row As Integer = 2
Dim col As Integer = 3
Dim value As String = GetCellValue(row, col)
If Not IsNothing(value) Then
MessageBox.Show(row & ", " & col & " = " & value)
Else
MessageBox.Show("Invalid Row/Col!")
End If
End Sub
Private Function GetCellValue(ByVal row As Integer, ByVal col As Integer) As String
If row >= 1 AndAlso row <= data.Count Then
Dim arr As String() = data(row - 1)
If col >= 1 AndAlso col <= arr.Length Then
Return arr(col - 1)
End If
End If
Return Nothing
End Function
私有数据作为新列表(字符串()的列表)
Private Sub LoadData()'调用它一次(可能是从Load()事件中调用?)
data.Clear()
csvPath=appPath&“/sample.csv”
使用sr作为新的StreamReader(csvPath)
而不是sr.EndOfStream
data.Add(sr.ReadLine().Split(“,”c))
结束时
终端使用
端接头
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
“这些是基于一个的价值观
将行设置为整数=2
Dim col作为整数=3
作为字符串的Dim值=GetCellValue(行、列)
如果不是Nothing(值),则
MessageBox.Show(行和列、列和值)
其他的
MessageBox.Show(“无效行/列!”)
如果结束
端接头
私有函数GetCellValue(ByVal行为整数,ByVal列为整数)为字符串
如果行>=1且行=1且列列以下是将该文件加载到内存并存储以供重复访问的示例:
Private data As New List(Of String())
Private Sub LoadData() ' call it ONCE (maybe from the Load() event?)
data.Clear()
csvPath = appPath & "/sample.csv"
Using sr As New StreamReader(csvPath)
While Not sr.EndOfStream
data.Add(sr.ReadLine().Split(","c))
End While
End Using
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' these are one based values
Dim row As Integer = 2
Dim col As Integer = 3
Dim value As String = GetCellValue(row, col)
If Not IsNothing(value) Then
MessageBox.Show(row & ", " & col & " = " & value)
Else
MessageBox.Show("Invalid Row/Col!")
End If
End Sub
Private Function GetCellValue(ByVal row As Integer, ByVal col As Integer) As String
If row >= 1 AndAlso row <= data.Count Then
Dim arr As String() = data(row - 1)
If col >= 1 AndAlso col <= arr.Length Then
Return arr(col - 1)
End If
End If
Return Nothing
End Function
私有数据作为新列表(字符串()的列表)
Private Sub LoadData()'调用它一次(可能是从Load()事件中调用?)
data.Clear()
csvPath=appPath&“/sample.csv”
使用sr作为新的StreamReader(csvPath)
而不是sr.EndOfStream
data.Add(sr.ReadLine().Split(“,”c))
结束时
终端使用
端接头
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
“这些是基于一个的价值观
将行设置为整数=2
Dim col作为整数=3
作为字符串的Dim值=GetCellValue(行、列)
如果不是Nothing(值),则
MessageBox.Show(行和列、列和值)
其他的
MessageBox.Show(“无效行/列!”)
如果结束
端接头
私有函数GetCellValue(ByVal行为整数,ByVal列为整数)为字符串
如果行>=1且行=1且列为列,则此CSV文件有多大?“…在给定行号和列号时?”是否将重复获取多个值?我们询问的原因是,如果您的数据相当“小”,那么您应该将其全部加载到内存中,这样您就可以在再次点击硬盘驱动器的情况下访问它。Steve,csv文件大约有25000行和4列显示此csv文件大吗?“…当给定行数和列数时?”您是否将重复获得多个值?我们询问的原因是,如果您的数据相当“小”,那么您应该将其全部加载到内存中,这样您就可以再次访问硬盘。Steve,csv文件大约有25000行和4列