visual free basic 通过ado连接excel文件。注意程序的位数和驱动程序的位数要保持一致。

#include once "Afx/CADODB/CADODB.inc"
DIM pRecordset AS CAdoRecordset
Dim r As CVAR = ""
Dim f As CAdoField
DIM Connstr AS  CVAR = "Provider=Microsoft.ace.OLEDB.12.0;Extended properties=excel 12.0;Data Source=C:\Users\FanXiaoLei\Desktop\1.xlsx"
pRecordset.CursorLocation = adUseClient
DIM SQLStr AS CVAR = "SELECT * FROM [数据$]"
DIM hr AS HRESULT = pRecordset.Open(SQLStr, Connstr, adOpenKeyset, adLockOptimistic, adCmdText)
PRINT pRecordset.RecordCount & "条记录"
Dim flds As CAdoFields = pRecordset.Fields
Dim cs As Long=flds.count-1
For c As Long = 0 To cs
   f.attach(flds.Item(c))
   r=r & !"\t" & f.Name
Next
PRINT r
DO While Not pRecordset.EOF
   r = ""
   For x As Long = 0 To cs
      r = r & !"\t" & pRecordset.Collect(x)
   Next
   PRINT CWSTR(r,CP_UTF8)
   IF pRecordset.MoveNext <> S_OK THEN EXIT DO
LOOP

 

运行结果: