【问题标题】:Using ADODB in VBScript to find the number of rows in an Excel sheet?在 VBScript 中使用 ADODB 查找 Excel 工作表中的行数?
【发布时间】:2011-10-28 19:57:18
【问题描述】:

我正在尝试使用 VBScript 中的 ADODB 来访问 Excel 文件,以查找给定工作表中已输入数据的行数。到目前为止,我的代码显示了工作表上的所有内容,但我不确定如何计算行数或使用查询直接找到行数。我想使用 ADODB,因为它不会直接打开 Excel 文件,但如果这不是最好的方法,那么我该怎么做呢?谢谢。

Set adodb = CreateObject("ADODB.Connection")
adodb.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
              "test.xls" & ";Extended Properties=""Excel 8.0;IMEX=1;" & _
              "HDR=NO;" & """"

Set result = adodb.Execute("Select * from [Sheet1$]")

MsgBox result.GetString 

result.Close
adodb.Close
Set adodb = Nothing
Set result = Nothing

【问题讨论】:

    标签: excel vbscript adodb


    【解决方案1】:

    为您的 Connection 对象添加 CursorLocation 属性。

    更新:

    'result.CursorLocation = 3 'adUseClient
    adodb.CursorLocation = 3 'adUseClient
    

    然后你可以得到行数。

    MsgBox result.RecordCount
    

    【讨论】:

    • 我收到错误:对象打开时不允许操作...对于 result.CursorLocation = 3
    • 对不起。一定是 adodb.CursorLocation = 3
    【解决方案2】:

    我让它工作正常:

    Sub testit()
    
    Dim ad As New adodb.Connection
    Dim result As New adodb.Recordset
    
        ad.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=test.xls ;" & _
        "Extended Properties=Excel 8.0;"
    
        result.Open "Select count(*) FROM [Sheet1$]", _
            ad, adOpenStatic, adLockOptimistic, adCmdText
    
        Debug.Print "rows:" & result.GetString
    
        result.Close
        ad.Close
    
    End Sub
    

    (我改了你的变量名adodb,好像有冲突)。

    【讨论】:

      猜你喜欢
      • 2014-04-12
      • 1970-01-01
      • 2011-01-06
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多