使用Excel过滤器或vb.net过滤Excel行

时间:2016-05-06 08:26:23

标签: vb.net excel interop ms-office

enter image description here

我有17728行这种格式,我只需要保留包含“User active”,“First Name”,Last Name“,”Group“,”24 bit card code“和”8,16 bit“的行卡代码“。

像:

用户有效:是

名字:Pharma

姓氏:访客1

...

这适用于文件中的所有17728行。

有没有办法通过行号来做到这一点?

如果我们将第3行用于“用户激活”并添加19,我们将登陆第二个“用户激活”行。 或者还有其他解决方案吗?

1 个答案:

答案 0 :(得分:1)

Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

    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.ApplicationClass
    xlWorkBook = xlApp.Workbooks.Open("c:\vbexcel.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
End Sub
End Class

来源:To read the entire worksheet in an Excel workbook through VB.net Code