当前位置:软件学习 > Excel >>

连接Excel数据源并查询

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

[vb]
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 


摘自 TX_OfficeDev的专栏
补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,