使用vb.net在Excel工作表中查找特定数据 [英] Find specific data in excel sheet using vb.net
本文介绍了使用vb.net在Excel工作表中查找特定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直试图通过WinForm应用程序在Excel中查找特定数据,然后尝试使用vb.net获取同一行中的下一个值.
I have been trying to find a specific data in Excel through a WinForm application and then try to fetch the next value in the same row using vb.net.
Example:
ElementName Value
Age 24
Name John Clan
Music Rock
假设我要查找年龄.然后我想让代码返回24,从Excel文件中搜索它.
Suppose if I want to find Age. then I want to make the code return as 24 searching it from Excel file.
我尝试将整个Excel数据提取到数据集中,但这没有帮助.
I tried pulling the whole Excel data into a Dataset but it wasn't helpful.
请引导我.
推荐答案
Imports Interop = Microsoft.Office.Interop.Excel
'Find In Excel
Private Function FindValueInExcel(ByVal sTextToFind)
Dim currentFind As Interop.Range = Nothing
Dim firstFind As Interop.Range = Nothing
Dim xlappFirstFile As Interop.Application = Nothing
Dim sSearchedValue As String
xlappFirstFile = CreateObject("Excel.Application")
xlappFirstFile.Workbooks.Open("D:\Sample.xlsx")
Dim rngSearchValue As Interop.Range = xlappFirstFile.Range("A1", "C5")
currentFind = rngSearchValue.Find(sTextToFind, , _
Interop.XlFindLookIn.xlValues, Interop.XlLookAt.xlPart, _
Interop.XlSearchOrder.xlByRows, Interop.XlSearchDirection.xlNext, False)
If Not currentFind Is Nothing Then
sSearchedValue = DirectCast(DirectCast(currentFind.EntireRow, Microsoft.Office.Interop.Excel.Range).Value2, System.Object)(1, 3).ToString()
Else
sSearchedValue = ""
End If
Return sSearchedValue
End Function
对我有帮助的链接- 查看全文