用vb打开的一个excel文件 如何得到它的行数及列数

解决方案 »

  1.   

    把Excel表当作数据表来读
        strFileName = dirname & filename & ".xls"
        adoConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & strFileName & ";Extended Properties='Excel 8.0;HDR=Yes'"
        adoRecordset.Open "select * from [tow.sheet$] where 塔号 is not null", adoConnection, adOpenKeyset, adLockOptimistic有多少记录多少字段,循环就可以知道了
      

  2.   

    mysheet.usedrange.rows.countPrivate Function GetExcelRowCount(strSourceFile as string ,strSourceTable as string ) as Integer    
        Dim cnSource As New ADODB.Connection
        Dim rsSource As New ADODB.Recordset   
        cnSourceDb.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strSourceFile & ";Extended Properties=Excel 8.0;Persist Security Info=true"
        rsSource.CursorLocation = adUseClient
        rsSource.Open "select * from [" & strSourceTable & "]", cnSource, adOpenKeyset, adLockOptimistic
      GetExcelRowCount= rsSource.RecordCount
      cnSource.close
      rsSource.close
      set cnSource=nothing
      set rsSource=nothing
    End Function调用:GetExcelRowCount( "d:\new.xls","Sheet1$")
      

  3.   

    以上是行数,至于列数只要将里面的返回值改为:
    GetExcelColCount=rsSource.Fields.Count