简体   繁体   English

使用VB.NET将数据从Excel工作表传递到DataGridView

[英]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. 我正在一个项目中,该项目旨在将数据从datagridview导入和导出到excel表,反之亦然。 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: 到目前为止,我设法将数据从datagridview发送到excel,但是我只是不能将数据从excel发送到datagrid ...我尝试了以下代码:

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...) 但是我一直遇到异常:(并且我尝试将项目的目标从Any CPU更改为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. System.Data.dll中发生了类型为'System.InvalidOperationException'的未处理异常。其他信息:Microsoft.ACE.OLEDB.12.0提供程序未在本地计算机上注册。

So: is there any way I could send data from excel to datagrid WITHOUT USING OLEDB? 所以:有什么方法可以不使用OLEDB而将数据从excel发送到datagrid? 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! 祝你有美好的一天! (: (:

Actually this is even better: 实际上,这甚至更好:

'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine 未在本地计算机上注册“ Microsoft.ACE.OLEDB.12.0”提供程序

See the selected answer there. 在此处查看所选答案。 Make sure to read the comments underneath it. 确保阅读其下方的注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM