使用vb.net在特定列中查找特定文本/字符串的行

时间:2014-01-23 11:12:47

标签: vb.net string excel row

我是Vb.net中的新手,想询问如何找到特定列中特定字符串的确切行号。例如:

1 CEBE 
2 START
3 Aico, Pilar
4 END

在上面的例子中,我想得到第2行的文本/字符串START的行号,以及第4行的END。我该怎么做?提前致谢。

1 个答案:

答案 0 :(得分:0)

按行和列循环代码计数。

    Dim xlApp As Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet
    Dim range As Excel.Range
    Dim rCnt As Integer
    Dim cCnt As Integer
    Dim Obj As Object

    xlApp = New Excel.Application
    xlWorkBook = xlApp.Workbooks.Open("c:\Schedule.xlsx")
    xlWorkSheet = xlWorkBook.Worksheets("sheet1")

    range = xlWorkSheet.UsedRange

    For rCnt = 1 To range.Rows.Count
        For cCnt = 1 To range.Columns.Count
            Obj = CType(range.Cells(rCnt, cCnt), Excel.Range)
            MsgBox(Obj.value)
        Next
    Next

    xlWorkBook.Close()
    xlApp.Quit()

    releaseObject(xlApp)
    releaseObject(xlWorkBook)
    releaseObject(xlWorkSheet)

End Sub

Private Sub releaseObject(ByVal obj As Object)
    Try
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        obj = Nothing
    Catch ex As Exception
        obj = Nothing
    Finally
        GC.Collect()
    End Try