Here is code I have in an app that allows user to select a spreadsheet and import, if it helps. It ended up being a little finicky depending on the OLE version installed on the PC but it worked fine. I had a good reason at the time to define the column names (forget what it was), you could just do a "*" in the select I am sure. In the end it just populates a datatable, I then loaded to a grid for user display and further processing.
I remover a lot of crud you wouldnt have cared about, hopefully nothing important
Private Sub loadSpreadsheet(ByVal millprefix As String)
Dim connstr As String = String.Empty
Dim selectStr As String = String.Empty
Dim colnames As String = String.Empty
Dim sheetname As String = String.Empty
Dim millprices As DataTable = New DataTable
millprices.TableName = "millprices"
millprices.Columns.Add("STYLE", System.Type.GetType("System.String")) millprices.Columns.Add("STYDSC", System.Type.GetType("System.String"))
millprices.Columns.Add("EFDT", System.Type.GetType("System.String"))
millprices.Columns.Add("PTYPDSC", System.Type.GetType("System.String"))
millprices.Columns.Add("UOM", System.Type.GetType("System.String"))
millprices.Columns.Add("CUTPRY", System.Type.GetType("System.Decimal"))
millprices.Columns.Add("ROLPRY", System.Type.GetType("System.Decimal"))
colnames = "STYLE,STYDSC,EFDT,PTYPDSC,UOM,CUTPRY,ROLPRY"
If UcGetFile1.SelectedFileExtension = ".csv" Then
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & UcGetFile1.SelectedFilePath & ";Extended Properties=Text;"
selectStr = "SELECT " & colnames & " FROM " & UcGetFile1.SelectedFilenameWithoutPath
Else
'read this, registry changes required in some version of JET install
'Hkey_Local_Machine/Software/Microsoft/Jet/4.0/Engines/Excel
'TypeGuessRows = 0
'ImportMixedTypes = Text
'http://blog.lab49.com/archives/196
sheetname = UcGetFile1.SelectedFilenameWithoutPath
Dim filelen As Integer
filelen = sheetname.Trim.Length
sheetname = sheetname.Substring(0, filelen - 4)
'connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & UcGetFile1.SelectedFilenameWithPath & ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';"
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
UcGetFile1.SelectedFilenameWithPath & _
";Extended Properties=""Excel 8.0;HDR=Yes;"";"
selectStr = "SELECT " & colnames & " FROM [" & sheetname & "$]"
End If
Dim sConnectionString As String = connstr
Dim objConn As New OleDbConnection(sConnectionString)
objConn.Open()
Dim objCmdSelect As New OleDbCommand(selectStr, objConn)
Dim objAdapter1 As New OleDbDataAdapter()
objAdapter1.SelectCommand = objCmdSelect
Dim objDataset1 As New DataSet()
objDataset1.Tables.Add(millprices)
objAdapter1.Fill(objDataset1, "millprices")
dgvSpreadsheetImport.DataSource = objDataset1.Tables(0).DefaultView
objConn.Close()
End Sub
Keith Chisarik