1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| Dim OpenFileDialog1 As New OpenFileDialog
Dim dt As New DataTable
If OpenFileDialog2.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim FileName As String = OpenFileDialog2.FileName.ToUpper
Dim Builder As New OleDbConnectionStringBuilder With {.DataSource = FileName}
' Setup the properties for our connection dependent on which file was open.
If IO.Path.GetExtension(FileName) = ".XLSX" Then
Builder.Provider = "Microsoft.ACE.OLEDB.12.0"
Builder.Add("Extended Properties", "Excel 8.0;HDR=No;")
Else
Builder.Provider = "Microsoft.Jet.OLEDB.4.0"
Builder.Add("Extended Properties", "Excel 8.0;HDR=No;")
End If
Using cn As New OleDbConnection With {.ConnectionString = Builder.ConnectionString}
Dim cmd As New OleDbCommand With _
{ _
.CommandText = "SELECT * FROM [Feuil1$]", _
.Connection = cn _
}
cn.Open()
dt.Load(cmd.ExecuteReader)
DataGridView1.DataSource = dt
ActiveControl = DataGridView1
End Using
End If
End Sub |
Partager