Dim objOpenFileDialog As New OpenFileDialog
'Set the Save dialog properties
With objOpenFileDialog
.DefaultExt = "xls"
'.FileName = "Test"
.Filter = "Microsoft Excel (*.xls)|*.xls|Microsoft Excel (*.xlsx)|*.xlsx|All files (*.*)|*.*"
.FilterIndex = 1
'.OverwritePrompt = True
.Title = "Open File Excel"
End With
If objOpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
' Try
'Open or Create the file
filePath = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, objOpenFileDialog.FileName)
Me.ImportExcel()
'Catch fileException As Exception
'Throw fileException
'End Try
End If
'Clean up
objOpenFileDialog.Dispose()
objOpenFileDialog = Nothing
Code (VB.NET)
Private Sub ImportExcel()
MsgBox(filePath)
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim DtSet As New DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & filePath & "';Extended Properties=Excel 8.0;")
'MyConnection = New System.Data.OleDb.OleDbConnection(" Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & filePath & ";Extended Properties=Excel 12.0;")
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
MyCommand.Fill(DtSet, "Sheet1")
DGV1.DataSource = DtSet.Tables(0)
MyConnection.Close()
End Sub
Error
Tag : .NET, Ms SQL Server 2005, Excel (Excel.Application), VB.NET