Question

I want to get the data displayed in the cell ( for example AA2 00:43.6), not the value that's written above 12:00:44 AM. How can I manage to do so without any API etc. Check this image from my excel 2010 example for more info:

Image from excel example

Here is my code for importing the excel file

MyConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & _
                          path & "';Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";") 
'i also tried IMEX=0

dtTimes = New DataTable
Dim sqlstrTimes As String = "SELECT [RACETIME] " & "FROM [SheetH$];"
'SheetF$
Try
    Dim MyCommandHistory As New OleDb.OleDbDataAdapter(sqlstrTimes , MyConnection)
    MyCommandHistory.Fill(dtTimes )
Catch ex As Exception
    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK
            , MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
Finally
    MyConnection.Close()
End Try

I checked the website of connectionstrings.com for excel-2010, but no luck there.

Was it helpful?

Solution

You can try it this way :

  1. Set connection string to read excel file without header (HDR=NO). Column name will be autogenerated for each column starting from F1 to Fn.
  2. Fill DataTable the way you currently do
  3. Remove first row from DataTable since it actually column header name in excel

With that you'll get the text instead of DateTime value from excel. In my understanding, that because excel determines data type from the first row. So with this hack the first row is a text (the header) hence decided that the column is of type text/string.

MyConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & _
                          path & "';Extended Properties=""Excel 12.0;HDR=NO;IMEX=1"";") 

dtTimes = New DataTable
Dim sqlstrTimes As String = "SELECT [F1] " & "FROM [SheetH$];"

Try
    Dim MyCommandHistory As New OleDb.OleDbDataAdapter(sqlstrTimes , MyConnection)
    MyCommandHistory.Fill(dtTimes)
Catch ex As Exception
    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK
            , MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
Finally
    MyConnection.Close()
End Try

dtTimes.Rows[0].Delete()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top