I am importing excel worksheets to a datagridview using the following code:
Private Sub Browsimportbtn_Click(sender As Object, e As EventArgs) Handles Browsimportbtn.Click
Dim textpath As String
Dim textpath1 As String
Dim opf As New OpenFileDialog
If opf.ShowDialog = 1 Then
textpath = opf.FileName
textpath1 = opf.SafeFileName
textpath1 = textpath1.Remove(textpath1.Length -4,4)
Dim cnexcell As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & textpath & "; Extended Properties = ""Excel 12.0 Xml;HDR=YES"";")
Dim cmdE As New OleDbCommand("SELECT * FROM Feuil1", cnexcell)
Try
Dim daoledb As New OleDbDataAdapter
Dim dset As New DataSet
daoledb.SelectCommand = cmdE
daoledb.Fill(dset, "Feuil1")
DGVmodele.DataSource = dset.Tables("Feuil1")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
The code above is working well with .XLSX
files (office 2007,2010...) but do not with .XLS
and i don't know where is the issue.
Any suggestions?
Try using this instead.
It is unadvisable to use the JET OLEDB because it is outdated. If you really want to use it, you might need to reinstall Access Database Engine 2003.
Also, I heard there's also a bug regarding this.
The issue is in the connectionstring.
You are specifying the
Excel 12
as a version in the connection string which is related to 2007 or higher. Try using this function to build the connectionstring