在VB编程中,如何读取Excel文件的数据?

解决方案 »

  1.   


    能否用VB的ADO的方法访问Excel文件,如果能该怎样实现?Dim adoConnection As New ADODB.Connection 
        Dim adoRecordset As New ADODB.Recordset 
        'OLE DB + ODBC Driver 方式: 
        'adoConnection.Open "Data Provider=MSDASQL.1;driver=Microsoft Excel Driver (*.xls);DBQ=e:\temp\book2.xls" 
        'Microsoft.Jet.OLEDB.4.0 方式,(建议) 
        adoConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=e:\temp\book2.xls;Extended Properties='Excel 8.0;HDR=Yes'" 
         
        adoRecordset.Open "select * from [sheet1$]", adoConnection, adOpenKeyset, adLockOptimistic 
        Debug.Print adoRecordset.RecordCount 
        Dim i As Integer 
        Do Until adoRecordset.EOF 
         For i = 0 To adoRecordset.Fields.Count - 1 
         Debug.Print adoRecordset.Fields.Item(0).Name 
         Debug.Print adoRecordset.Fields.Item(0).Value 
         Next i 
         adoRecordset.MoveNext 
        Loop 
         
        '注: OLE DB + ODBC Driver 方式不支持以下语句,但 Microsoft.Jet.OLEDB.4.0 方式支持! 
        adoConnection.Execute "insert into [sheet1$](F1) values(3)" 
        adoRecordset.AddNew Array("f1", "f2", "f3", "f4"), Array(1, 2, 3, 4) 
      

  2.   


    微软的答案:
    http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q278/9/73.asp&NoWebContent=1