Excel to DataGridView

  • Thread starter Thread starter Niknik C
  • Start date Start date
N

Niknik C

Guest
Hi!

I have a problem importing Excel file into DataGridView.

I cannot see any error importing, when I upload the file nothing happens.


Here's the code:

Private Sub SAVE_Click(sender As Object, e As EventArgs) Handles SAVE.Click

Try
OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
OpenFileDialog.Filter = "All Files (*.*)|*.*|Excel Files(*.xlsx)|*.xlsx|xls Files(*.xls)|*.xls"

If OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
Dim fi As New IO.FileInfo(OpenFileDialog.FileName)
Dim Filename As String = OpenFileDialog.FileName
excel = fi.FullName
conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excel + ";Extended Properties=Excel 12.0")
dta = New OleDbDataAdapter("Select*from[Sheet1$]", conn)
dts = New DataSet
dta.Fill(dts, "[Sheet1$]")
DGV1.DataSource = dts
DGV1.DataSource = "[Sheet1$]"
conn.Close()
End If
Catch ex As Exception
MsgBox(ex.Message)
conn.Close()
End Try

I am using Visual Studio 2019 and Excel Professional 2019.

Thank you for the advance help.

-Niknik

Continue reading...
 
Back
Top