连接Excel数据源并查询

Excel表必须是97-2003格式的。否则无法查询。

Imports System.Data.OleDb

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As  _
                              System.EventArgs) Handles Button1.Click
        Dim ConnectiongString As String
        Dim Path As String
        Dim Dialgo As OpenFileDialog = New OpenFileDialog
        Dim Connection As OleDbConnection = New OleDbConnection
        Dim adapter As OleDbDataAdapter

        Dialgo.Filter = "Excel Document (*.xls)|*.xls"
        Dialgo.ShowDialog()
        Path = Dialgo.FileName
        ConnectiongString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
            + Path + ";Extended Properties='Excel 8.0;HDR=no;IMEX=0'"
        Connection.ConnectionString = ConnectiongString
        Connection.Open()
        adapter = New OleDbDataAdapter("SELECT * FROM [Sheet1$] Where B = 4",
                                       Connection)
        adapter.Fill(DataSet1)
        MsgBox(DataSet1.Tables(0).Rows(0).Item(0))
        Connection.Close()
    End Sub
End Class

相关政策:http://download.csdn.net/detail/tx_officedev/4039787
 

你可能感兴趣的:(Excel)