2012年12月21日 星期五

vbnet 讀取 exel


參考引用:How to import exel file to sql server using VB.Net
--- 

Try
            Dim fBrowse As New OpenFileDialog
            With fBrowse
                .Filter = "Excel files(*.xls)|*.xls|All files (*.*)|*.*"
                .FilterIndex = 1
                .Title = "Import data from Excel file"
            End With
            If fBrowse.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Dim fname As String
                fname = fBrowse.FileName
                Dim MyConnection As System.Data.OleDb.OleDbConnection
                Dim DtSet As System.Data.DataSet
                Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
                MyConnection = New System.Data.OleDb.OleDbConnection _
                ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & fname & ";" & "Extended Properties=""Excel 8.0;HDR=NO;IMEX=1""")
                MyCommand = New System.Data.OleDb.OleDbDataAdapter("select Columns names from [sheet$]", MyConnection)
               
                MyCommand.TableMappings.Add("Table", "Ur table name.")
                DtSet = New System.Data.DataSet
                MyCommand.Fill(DtSet)
'If dataset is not empty Then write code here to insert values to DB.
                MyConnection.Close()
            End If
  Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

沒有留言:

張貼留言