如何设置过滤器之前我红色excel文件vb.net?

时间:2015-10-29 10:23:04

标签: vb.net excel

我有一个有90.000行的excel,但当我过滤我的专栏" H1"通过" Sample Shop",我有2000行。我想读2000行而不是90000.在我读取vb.net中的excel文件之前如何设置过滤器? 我的代码是:

 Do While currentWorksheet.Cells(i, 1).Value <> Nothing
If workBook.Worksheets.Count > 0 Then
                Name = currentWorksheet.Cells(i, 1).Value
                DataSo = currentWorksheet.Cells(i, 33).Value
                Try
                    Dim Conv As Double = Double.Parse(DataSo)
                    Variable = DateTime.FromOADate(Conv).ToString("MMMM/dd/yyyy")
                Catch ex As Exception

                End Try

                Data1 = Convert.ToDateTime(BOX_Data1.Text)
                Data2 = Convert.ToDateTime(BOX_Data2.Text)

                If (currentWorksheet.Cells(i, 4).Value) = "Completed" And (currentWorksheet.Cells(i, 8).Value) = "RBT SAMPLE SHOP" Then

                    If Variable >= Data1 And Variable <= Data2 Then

                        If currentWorksheet.Cells(i, 35).Value <> Nothing Or currentWorksheet.Cells(i, 35).Value = "0.00" Then
                            'a = String.Format("{0:N2}", Double.Parse(a))
                            a = a + currentWorksheet.Cells(i, 35).Value
                            x1 = x1 + 1
                        End If
                        If currentWorksheet.Cells(i, 38).Value <> Nothing Or currentWorksheet.Cells(i, 38).Value = "0.00" Then
                            'b = String.Format("{0:N2}", Double.Parse(b))
                            b = b + currentWorksheet.Cells(i, 38).Value
                            x2 = x2 + 1
                        End If
                        If currentWorksheet.Cells(i, 41).Value <> Nothing Or currentWorksheet.Cells(i, 41).Value = "0.00" Then
                            'c = String.Format("{0:N2}", Double.Parse(c))
                            c = c + currentWorksheet.Cells(i, 41).Value
                            x3 = x3 + 1
                        End If
                    End If
                End If
                i = i + 1
            Loop

1 个答案:

答案 0 :(得分:0)

您可以使用类似的代码:

Dim oledbConnectionString As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
    & "DATA SOURCE=YourExcelFilePath.xls;" _
    & "EXTENDED PROPERTIES='Excel 8.0;HDR=No'"

Dim oledbConn As New OleDbConnection(oledbConnectionString)
oledbConn.Open()

Dim oledbCommand As OleDbCommand = oledbConn.CreateCommand()
oledbCommand.CommandText = "SELECT * " _
    & "FROM [YourExcelSheetName$] " _
    & "WHERE [F8] LIKE @filter + '%'"

oledbCommand.Parameters.Add("@filter", OleDbType.VarChar).Value = "sample shop"

Dim oledaXLSDaCaricare As New OleDbDataAdapter(oledbCommand)

Dim dtTest As New DataTable

Try
    oledaXLSDaCaricare.Fill(dtTest)
Catch ex As Exception
    MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try

oledbConn.Close()
oledbConn.Dispose()

一些注意事项:

  • HDR=N o表示第一行不包含列名;也许你必须在HDR=Yes
  • 中改变它
  • [F8]是对工作表中第8列的引用,因此H