哪位大侠有使用CDaoDatabase(Jet)或ADO访问Excel的代码实例,共享一下,呵呵.或者能解决ODBC中不显示第一条记录的方法也行。还有不使用第一列的字段名怎么Select Excel中的一列?

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/3487/3487468.xml?temp=.5411646
    http://community.csdn.net/Expert/topic/3383/3383123.xml?temp=.8532373
      

  2.   

    用ADO连接Excel跟连接其他数据库的方法一样,只是连接字符串不同,修改一下就可以了。。
    Excel的连接字符串如下:
    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""注释: 
    "HDR=Yes;" indicates that the first row contains columnnames, not data
    "IMEX=1;" tells the driver to always read "intermixed" data columns as text
    TIP! SQL syntax: "SELECT * FROM [sheet1$]" - i.e. worksheet name followed by a "$" and wrapped in "[" "]" brackets.ADO记录集打开以后,就可以用m_pRs->GetCollect(0);来访问第一个字段的数据。。
    ADO参考文章:http://www.vckbase.com/document/viewdoc/?id=1215
      

  3.   

    呵呵,谢谢了,上面的两位大侠,还有一个疑问,怎么写类似"SELECT A1,B1,D1 FROM [sheet1$]"的查询语句 --A1,B1,D1是Excel中的列名
      

  4.   

    跟平时一样的写啊,没有什么区别的,你把Excel中的列当成数据库中的字段,Excel页签当成数据库的表操作就可以了如:SELECT A1,B1,D1 FROM [sheet1]  就是把sheet1中的A1,B1,D1三列数据选出来。。
      

  5.   


    #include <afxwin.h>
    #import "c:\program files\common files\system\ado\msado15.dll" \
    no_namespace \
    rename ("EOF", "adoEOF")
    #include <iostream.h>
    void main()
    {
    CoInitialize(NULL); _ConnectionPtr pConnection;
    pConnection.CreateInstance(__uuidof(Connection));
    try                 
    {
    pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Test.xls;Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\"","","",adModeUnknown);
    }
    catch(_com_error e)
    {
    cout << e.ErrorMessage() << endl;
    //pConnection->Release();
    //pConnection = NULL;
    CoUninitialize();
    return;
    }

    _RecordsetPtr pRecordset;
    pRecordset.CreateInstance(__uuidof(Recordset));
    try
    {
    //pRecordset->Open("SELECT * FROM [Sheet1$]", pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
    pRecordset->Open("SELECT A,B FROM [Sheet1$]", pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
    }
    catch(_com_error *e)
    {
    cout << e->ErrorMessage() << endl;
    //pRecordset->Release();
    //pRecordset = NULL;
    pConnection->Close();
    //pConnection->Release();
    //pConnection = NULL;
    CoUninitialize();
    return;
    } _variant_t var;
    CString strName;
    while(!pRecordset->adoEOF)
    {
    var = pRecordset->GetCollect(long(0));
    if(var.vt != VT_NULL)
    strName = (LPCSTR)_bstr_t(var);
    cout << strName << "\t"; var = pRecordset->GetCollect(long(1));
    if(var.vt != VT_NULL)
    strName = (LPCSTR)_bstr_t(var);
    cout << strName << endl;

    pRecordset->MoveNext();
    }
    pRecordset->Close();
    //pRecordset.Release();
    //pRecordset = NULL;

    if(pConnection->State)
            pConnection->Close();
    //pConnection->Release();
    //pConnection = NULL;
    CoUninitialize();
    }我的一个简易程序,SELECT*就一切正常,SELECT A,B就错误,还请大侠指点
      

  6.   

    改成select [A],[B]..... 试试
      

  7.   

    Select [A],[B]....或者Select [A$],[B$]都不行
      

  8.   

    给个邮箱,发给你操作excel的程序。