如题,谢谢。  _ConnectionPtr   m_pConnection; 
  _RecordsetPtr     m_pResultRecordset; try 
    { 
  CString str="Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel 8.0;HDR=yes;IMEX=1\"; Data Source="+DBPathName+";";
          HRESULT   hr=m_pConnection.CreateInstance(__uuidof(Connection)); 
          m_pConnection-> Open((LPCSTR)str, " ", " ",adModeUnknown); 
      } 
catch(_com_error   &e) 
    { 
          MessageBox(e.Description()); 
    } 
  CString   m_strSQL= "select   *   from   [Sheet1$] "; 
  try 
    { 
            m_pResultRecordset.CreateInstance(__uuidof(Recordset)); 
            m_pResultRecordset-> Open(m_strSQL.AllocSysString(),
m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText); 
      } 
    catch(_com_error   &e) 
    { 
          MessageBox(e.Description()); 
      } 
这样连接有问题吗????弹出窗口“不是有效帐户和密码” 连接无法用于此操作,它在上下文中关闭或者无效

解决方案 »

  1.   

      m_pConnection-> Open((LPCSTR)str, " ", " ",adModeUnknown); 就报异常啦。。
      

  2.   

    操作excel不要通过数据库的方式
    建议使用com的方式
    http://blog.sina.com.cn/s/blog_4504388f0100073f.html
      

  3.   

    给你一段代码_ConnectionPtr pConnection;
    _RecordsetPtr pRecordset;
    CString tableName;
    pConnection.CreateInstance("ADODB.Connection");//两种初始化只能ADO只能指针的方式
    pRecordset.CreateInstance (__uuidof(Recordset));
    CString adoinfo;
    //m_PathName是Excel文件的全路径
    adoinfo.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Extended Properties=Excel 8.0"),m_PathName);//连接Excel2003,其它版本连接串不一样
    try                 
    {  
    pConnection->Open((_bstr_t)adoinfo,"","",adModeUnknown);

    //读取表名
    pRecordset = NULL;
    pRecordset = pConnection->OpenSchema(adSchemaTables);
    _bstr_t table_name;
    CStringArray table_array;
    while(!pRecordset->adoEOF)//获得表名
    {
    table_name = pRecordset->Fields->GetItem("TABLE_NAME")->Value;
    tableName=(LPCSTR)table_name;
    pRecordset->MoveNext();
    table_array.Add(tableName);
    }
    for(int inTable=0;inTable<table_array.GetSize();inTable++)//循环查询工作表,选择出储存数据的工作薄
    {
    pRecordset->Close();
    pRecordset.Release();
    pRecordset=NULL;
    pRecordset.CreateInstance (__uuidof(Recordset));//每次Release连接对象以后,都要重新初始化连接对象。
    CString strSQL;
    strSQL.Format("SELECT * FROM [%s]",table_array.GetAt(inTable));
    try
    {
    pRecordset->Open(_bstr_t(strSQL),                // 查询DemoTable表中所有字段
    pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
    adOpenForwardOnly,//adOpenDynamic,adOpenKeyset
    adLockOptimistic,
    adCmdText);

    }
    catch(_com_error e)
    {
    EndWaitCursor(); 
    AfxMessageBox(e.Description()); 
    return false;
    }
    //到这里就获取了一个Excel文件中一个表的所有数据的记录。
    // pRecordset->GetFields()->GetCount (); 获取表中的字段数
    //获取序号为index的字段名
    //b_FieldName=pRecordset->GetFields()->GetItem(_variant_t(long(index)))->GetName()
    //获取指定当前记录指定字段(井名)的值
    //pRecordset->GetFields()->GetItem(_variant_t("井名"))->Value
    //pRecordset->MoveNext();移动到下条记录
    //pRecordset->MoveFirst() 移动到第一条记录。
    大致就用这些函数。你试试
      

  4.   


    很奇怪,刚开始运行还很好,后来运行
           pConnection->Open((_bstr_t)adoinfo,"","",adModeUnknown);这里出异常
      

  5.   

    这个连接串是连接Excel2003的,对2007连接不上。
      

  6.   


    可以读取了,非常感谢啦,但为又有一个问题哈、
    strData   =   pRecordset-> Fields-> Item["项目名称"]-> Value;
    这样写完全没有问题,如果Item[]里面用变量代替,应该如何转化的呢?
    如下代码中 
    strData  =   pRecordset-> Fields-> Item[_bstr_t((LPCTSTR)planprocess->GetAt(len))]-> Value;好像读取不了。。planprocess是一个CStringArray的指针获取变量放在item[]中。。
    for(int len =0 ; len<planprocess->GetSize(); len++){
    MessageBox((_bstr_t)planprocess->GetAt(len));
       strData  =   pRecordset-> Fields-> Item[_bstr_t((LPCTSTR)planprocess->GetAt(len))]-> Value;
    str =(LPCTSTR)strData;
    MessageBox(str);
    }
    LPCTSTR, _T,_bstr_t,_variant_t这四个关系完全不清楚求解释。。