1
Private Sub readexcel()
    Dim cnn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & txtFileName.Text & "; Extended Properties=Excel 12.0;")

    Dim oconn As New OleDbCommand("select * from [Sheet1$]", cnn)
    cnn.Open()
    Dim adp As New OleDbDataAdapter(oconn)
    Dim dt As New DataTable()
    Dim ds As DataSet
    adp.Fill (dt)

    dgvExcelData.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    dgvExcelData.EditMode = DataGridViewEditMode.EditProgrammatically
    dgvExcelData.DataSource = dt
End Sub

The above code reads Excel into Grid View,but some data will missing. Could someone tell me why?

enter image description here

8
  • Try it with IMEX=1 in your connection string. IMEX=1; tells the driver to always read "intermixed" (numbers, dates, strings etc) data columns as text. See this Commented Apr 26, 2013 at 19:13
  • @SiddharthRout Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & txtFileName.Text & "; Extended Properties=Excel 12.0;IMEX=1; correct? Commented Apr 26, 2013 at 19:18
  • @SiddharthRout Error Could not find installable ISAM. Commented Apr 26, 2013 at 19:19
  • @SiddharthRout is Excel u know? not Access Commented Apr 26, 2013 at 19:22
  • @SiddharthRout same,data still missing, i uploaded my .xlsx dl-web.dropbox.com/get/… Commented Apr 26, 2013 at 19:42

1 Answer 1

2

By default, ACE reads the first 8 rows of data and use that to decide the data type of each column.

If the data in those 8 rows in a column is of the same type then that type is assumed else it will assume text by default. After 8 columns it doesn't check it.

If your columns have mixed data types then you will have to make a small change in the registry.

Go to

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Office\14.0\Access Connectivity Engine\Engines\Excel

and change the value of the key TypeGuessRows from 8 to 0

enter image description here

Now test the below code

Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
    Dim strCon As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & txtFileName.Text &
                           "; Extended Properties=""Excel 12.0 XML;HDR=YES;IMEX=1;"""

    Dim cnn As New OleDbConnection(strCon)

    Dim oconn As New OleDbCommand("select * from [Sheet1$]", cnn)
    cnn.Open()

    Dim adp As New OleDbDataAdapter(oconn)
    Dim dt As New DataTable()
    adp.Fill(dt)

    dgvExcelData.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    dgvExcelData.EditMode = DataGridViewEditMode.EditProgrammatically
    dgvExcelData.DataSource = dt
End Sub

And this is the output

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

how come the NO K/P LAMA are auto rounded...example 8051815355155 will become 8051815355160
Sorry? I am not sure what are you asking?
change the NO KP LAMA to Number Format, then the number will auto rounded...why?(i have saved the dataset data to excel file)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.