Gabriel Stancu
Gabriel Stancu

Reputation: 4340

Passing data from Excel Sheet to DataGridView using VB.NET

I am working on a project that aims to import and export data from a datagridview to excel table and viceversa. So far, I managed to send data from my datagridview to excel, but I just can't do it from excel to datagrid... I tried the following code:

Dim dtSheet1 As New DataTable
    Using cn As New System.Data.OleDb.OleDbConnection
        Dim Builder As New OleDbConnectionStringBuilder With
            {
                .DataSource = filename,
                .Provider = "Microsoft.ACE.OLEDB.12.0"
            }
        Builder.Add("Extended Properties", "Excel 16.0; IMEX=1;HDR=Yes;")
        cn.ConnectionString = Builder.ConnectionString

        cn.Open()

        Using cmd As OleDbCommand = New OleDbCommand With {.Connection = cn}
            cmd.CommandText = "SELECT * FROM [Sheet1$]"
            Dim dr As System.Data.IDataReader = cmd.ExecuteReader

            dtSheet1.Load(dr)
            datagridview1.DataSource = dtSheet1
        End Using
    End Using

But I keep encountering the exception: (and I tried changing the target of my project from Any CPU to x64...)

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll Additional information: The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.

So: is there any way I could send data from excel to datagrid WITHOUT USING OLEDB? If so, I would be so grateful for a solution that could get me out of this, already working on this for 3 days...

Have nice day! (:

Upvotes: 0

Views: 485

Answers (1)

Derek
Derek

Reputation: 8793

Actually this is even better:

'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine

See the selected answer there. Make sure to read the comments underneath it.

Upvotes: 1

Related Questions