无法访问excel文件

时间:2017-03-04 10:27:28

标签: excel vb.net visual-studio datagridview

enter image description here

我在Visual Studio中将数据从excel文件导入datagridview 提示错误为

  

无法更新数据库或对象是只读的

当我执行程序时。到目前为止我已经完成的步骤

  • 已安装的ACCESSDATABASE ENGINE
  • 使用CONFIG.MANAGER为X86平台提供设置
  • 授予当前用户所有使用正确文件的正确单击和属性所需的许可 还有其他办法可以解决这个问题吗?

Screenshot Of Output

Public Class ExcelData

Private Sub ExcelData_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Try

        Dim MyConnection As System.Data.OleDb.OleDbConnection
        Dim dataSet As System.Data.DataSet
        Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
        Dim path As String = "& TextBox2.text &"

        MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;")
        MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)

        dataSet = New System.Data.DataSet
        MyCommand.Fill(dataSet)
        DataGridView1.DataSource = dataSet.Tables(0)

        MyConnection.Close()
    Catch ex As Exception
        MsgBox(ex.Message.ToString)
    End Try

End Sub
End Class

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

我可能对此不正确,但下面的行看起来很奇怪......

Dim path As String = "& TextBox2.text &"

这会创建一个名为path的字符串,其值为:& TextBox2.text & ...

然后在path中使用此OleDbConnection字符串:

MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;")

在上面的一行中,您尝试连接DataSource名为"& TextBox2.text& ... ???

我猜测代码表格TextBox2.Text上的文本框包含您要打开的Excel文件的完整路径。如果是这种情况,那么只需更改变量字符串path,如下所示:

Dim path As String = TextBox2.Text

只是一个想法。