使用vb.net查找Excel表格中的特定数据

我一直试图通过WinForm应用程序在Excel中查找特定数据,然后尝试使用vb.net获取同一行中的下一个值。

Example: ElementName Value Age 24 Name John Clan Music Rock 

假设,如果我想find年龄。 那么我想让代码返回为24从Excel文件中search它。

我尝试将整个Excel数据拖入数据集,但没有帮助。

请引导我。

 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 

链接帮助了我 – http://msdn.microsoft.com/en-us/library/e4x1k99a.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1