0

我想尝试使用 ado 连接和 vba 将 excel 连接到 excel。但问题是它给出了一个错误,找不到可安装的 ISAM。我试图查看其他解决方案,但同样的问题将返回。可能有一个我缺少的activex控件吗?这是我的代码

Dim cN As ADODB.Connection '* Connection String
Dim RS As ADODB.Recordset '* Record Set
Dim sQuery As String '* Query String
Dim i1 As Long
Dim lMaxRow As Long '* Last Row in the Sheet
Dim iRevCol As Integer '*
Dim i3 As Integer

Set cN = New ADODB.Connection
cN.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\PC\Desktop\Excel Programming\PlayerDatabase.xlsm;Readonly=False;Extended Properties=Excel 12.0;;HDR=yes;Persist Security Info=False"
cN.ConnectionTimeout = 40
cN.Open

Set RS = New ADODB.Recordset

lMaxRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
sQuery = "Select * From [Player$]"


RS.ActiveConnection = cN
RS.Source = sQuery
RS.Open

If RS.EOF = True And RS.BOF = True Then
    MsgBox ("End of File")
End If

If RS.State <> adStateClosed Then
RS.Close
End If

If Not RS Is Nothing Then Set RS = Nothing
If Not cN Is Nothing Then Set cN = Nothing

更新:

现在我将我的连接字符串更改为此

cN.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\PC\Desktop\Excel Programming\PlayerDatabase.xlsm;Extended Properties='Excel 12.0 Macro;HDR=YES'"

但它给了我错误无法更新。数据库或对象是只读的。

当我把 readonly=false

cN.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\PC\Desktop\Excel Programming\PlayerDatabase.xlsm;ReadOnly=false;Extended Properties='Excel 12.0 Macro;HDR=YES'"

它会给出一个错误,因为找不到可安装的 ISAM :(

4

2 回答 2

0

解决如下:

string ConeectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtFlp.Text + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\"");
OleDbConnection oconn = new OleDbConnection(ConeectionString);
于 2014-09-17T08:59:05.333 回答
0

我不知道您XLSM从中检索数据的文件中有什么,但您的连接字符串应该尽可能简单。这对我有用(但我没有检查只读参数):

"Provider=Microsoft.ACE.OLEDB.12.0;" & _
           "Data Source=C:\Users\Dane\BazaDanych.xlsm;" & _
           "Extended Properties=Excel 12.0 Macro"
于 2013-08-02T09:53:27.607 回答