不连接ACCESS,就要连接EXCEL,谁能帮我啊?

解决方案 »

  1.   

    dim ac_Excel as new adodb.connection
    dim ar_Tmp   as new adodb.recordset
    '建立与EXCEL文件的连接
    with ac_Excel
         .CursorLocation = adUseClient
         If .State = adStateOpen Then .Close
         .ConnectionString = "Data Provider=MSDASQL.1;driver=Microsoft Excel Driver (*.xls);DBQ=c:\test.xls"
         .Open
         .CommandTimeout = 300
    end with
        
    '打开sheet$表
    If ar_Tmp.State = adStateOpen Then ar_Tmp.Close
    ar_Tmp.Open " select * from [sheet1$]", ac_Excel, adOpenKeyset, adLockReadOnly
      

  2.   

    Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & App.Path & "\test.xls;Extended Properties='Excel 8.0;HDR=Yes'"
      

  3.   

    Option Explicit      Dim cnn As ADODB.Connection
          Dim Rs_db As ADODB.Recordset
          Dim PathName  As String
          Dim strcnn As String
          Dim strsql_db As String
          Dim j As Integer    Private Sub Form_Load()
        On Error GoTo Err
        PathName = "D:\WorkBook.xls"      strcnn = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=false;Data Source=" & PathName & ";Extended Properties='Excel 8.0;HDR=Yes'"
          Set cnn = New ADODB.Connection
          cnn.Open strcnn
          strsql_db = "select * from [6月$]"
          cnn.Execute strsql_db
          Set Rs_db = New ADODB.Recordset
          Rs_db.Open strsql_db, cnn, adOpenStatic, adLockOptimistic
          
          For j = 0 To Rs_db.RecordCount - 1                     'j=1 是从excel的第二行读起
    '          MsgBox Rs_db.Fields(0).Value & "Null"              '当j=1时,是读取第一列的数据
               Rs_db.Fields(1).Value = "Hello"
    '          MsgBox Rs_db.Fields(2).Value & "Null"
    '          MsgBox Rs_db.Fields(3).Value & "Null"
    '          MsgBox Rs_db.Fields(4).Value & "Null"
    '          MsgBox Rs_db.Fields(5).Value & "Null"
    '          MsgBox Rs_db.Fields(6).Value & "Null"
    '          MsgBox Rs_db.Fields(7).Value & "Null"
              
              Rs_db.MoveNext
          Next j
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    Err:
         Debug.Print Err.Description
        End Sub需要引用ADO 2.X
      

  4.   

    faysky20: 
      我把Adodc1.ConnectionString设置为Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & App.Path & "\test.xls;Extended Properties='Excel 8.0;HDR=Yes'后,记录源设为2,选择好工作表,却出现 FROM子句语法错误的提示,怎么解决?
      

  5.   

    不要手动绑定,用代码来控制就不会出错了Private Sub Form_Load()
        Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & App.Path & "\test.xls;Extended Properties='Excel 8.0;HDR=Yes'"
        Adodc1.RecordSource = "select * from [Sheet1$]"
        Set DataGrid1.DataSource = Adodc1
    End Sub
      

  6.   

    ADODB也可以访问,ADODC也可以绑定,只不过工作表要用SQL语句,但问题是为何三个记录的表却只显示后两个,第一条不能显示呢?