2012年1月31日 星期二

vbnet 讀取 excel 採用 OleDb dataset


參考引用來源
-- 
Dim MyConnection As System.Data.OleDb.OleDbConnection

Dim myPath As String = "D:\CourseFootprints.xls"

Try

   Dim DS As System.Data.DataSet

   Dim strPath, strSheet As String

  strPath = "D:\Test.xls"

  strSheet = "Sheet1"

  Dim MyCommand As System.Data.OleDb.OleDbDataAdapter

  MyConnection = New System.Data.OleDb.OleDbConnection( _

  "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & strPath & ";Extended Properties=""Excel 8.0;IMEX=1;HDR=YES;""")

  ' Select the data from Sheet1 of the workbook.

  MyCommand = New System.Data.OleDb.OleDbDataAdapter( _

  "select * from [" & strSheet & "$]", MyConnection)

  DS = New System.Data.DataSet

  MyCommand.Fill(DS)

  Dim dv As DataView

  dv = DS.Tables(0).DefaultView

  dgGridVw.DataSource = dv

  MyConnection.Close()

Catch ex As Exception

       MsgBox(ex.Message)

End Try

沒有留言:

張貼留言