vfb查询excel

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

#include once "Afx/CADODB/CADODB.inc"
DIM pRecordset AS CAdoRecordset
Dim r As CVAR = ""
Dim f As CAdoField
Dim arr() As CVAR
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
ReDim arr(1 To flds.count)
For c As Long = 1 To flds.count
   f.attach(flds.Item(c-1))
   arr(c)=f.Name
   r=r & !"\t" & f.Name
Next
PRINT r
DO While Not pRecordset.EOF
   r = ""
   For x As Long = LBound(arr) To UBound(arr)    
      r = r & !"\t" & pRecordset.Collect(arr(x))
   Next
   PRINT CWSTR(r,CP_UTF8)
   IF pRecordset.MoveNext <> S_OK THEN EXIT DO
LOOP

运行结果:

vfb查询excel_第1张图片

你可能感兴趣的:(freebasic)