使用 OleDbDataAdapter讀入 Excel 檔

1 篇文章 / 0 new
author
使用 OleDbDataAdapter讀入 Excel 檔
Dim oleConn As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=檔案名稱; Extended Properties=""Excel 12.0; HDR=No; IMEX=1""")
Dim lawList As New System.Data.DataTable
oleConn.Open()
lawList = oleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
If lawList Is Nothing Then
Else
    lawList.Dispose()
    Dim dataAdp As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$A203:D2000]", oleConn)
    lawList = New System.Data.DataTable
    dataAdp.Fill(lawList)
    oleConn.Close()
    For i = 1 To 10
        Debug.Print(i & ":" & lawList.Rows(i).Item(0).ToString & " - " & lawList.Rows(i).Item(1).ToString)
    Next
End If
►選取 Sheet 內容方式 (須為excel內實際名稱)
全部 : "select * from [Sheet1$]"
區域 : "select * from [Sheet1$A2:D4]"
Free Web Hosting