Imports Microsoft.Office.Interop
Imports System.Data.OleDb
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strPath As String = "C:\Documents and Settings\Administrator\桌面\訂單.xls"
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";Extended Properties=Excel 8.0;"
Dim strSQL As String = "SELECT * FROM [Sheet1$]"
Dim excelConnection As New Data.OleDb.OleDbConnection(connectionString)
excelConnection.Open() ' this will open excel file
Dim dbCommand As OleDbCommand = New OleDbCommand(strSQL, excelConnection)
Dim dataAdapter As OleDbDataAdapter = New OleDbDataAdapter(dbCommand)
' create data table
Dim dTable As DataTable = New DataTable()
dataAdapter.Fill(dTable)
' bind the datasource
dataBingingSrc.DataSource = dTable
' assign the dataBindingSrc to the DataGridView
DataGridView1.DataSource = dataBingingSrc
' dispose used objects
dTable.Dispose()
dataAdapter.Dispose()
dbCommand.Dispose()
excelConnection.Close()
excelConnection.Dispose()
End Sub
End Class
留言列表