这段代码(ado访问excel文件和写入数据)可以在win7(32bit)系统上运行,并且可以在win7(64bit)上编译和链接,但是无法写入数据,为什么?

这段代码(ado访问excel文件和写入数据)可以在win7(32bit)系统上运行,并且可以在win7(64bit)上编译和链接,但是无法写入数据,为什么?

问题描述:

//#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename ("EOF", "adoEOF")  write at stdafx.h 
 
CoInitialize(NULL);
_ConnectionPtr  pCon_ex;
 void write_excel(CString dest_file)
 {

 ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";   
 ConnectionString += excel_path;   //excel   file   name   
 ConnectionString += ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=0\""; 
 BSTR resultsString = ConnectionString.AllocSysString();
 pCon_ex.CreateInstance(__uuidof(Connection));
 pCon_ex->Open(resultsString,"","",adModeUnknown);
  _RecordsetPtr m_ownpRecordset;
   m_ownpRecordset.CreateInstance(__uuidof(Recordset));
  try
  {
     m_ownpRecordset->Open("SELECT * FROM [清单$]",// 查询道路表中所有字段
     pCon_ex.GetInterfacePtr(),  // 获取库接库的IDispatch指针
     adOpenDynamic,
     adLockOptimistic,
     adCmdText);
  }
  catch(_com_error *e)

  {
   AfxMessageBox(e->ErrorMessage());
  }
 try
  {
   
    m_ownpRecordset->MoveFirst();
       int max_count=20;
		for(int i=0;i< max_count;i++)
		{
         m_ownpRecordset->AddNew();  
        CString astr=BDialog->m_StandrdList.GetItemText(i,0);
        CString bstr=BDialog->m_StandrdList.GetItemText(i,1);
	CString estr=BDialog->m_StandrdList.GetItemText(i,3);
	CString fstr=BDialog->m_StandrdList.GetItemText(i,4);
		
	 _variant_t t = _variant_t(long(0));
    m_ownpRecordset->PutCollect(&t,_variant_t(astr));  
	  t = _variant_t(long(1));
    m_ownpRecordset->PutCollect(&t,_variant_t(bstr));  
	 t = _variant_t(long(4));
    m_ownpRecordset->PutCollect(&t,_variant_t(estr));  
	t = _variant_t(long(5));
    m_ownpRecordset->PutCollect(&t,_variant_t(fstr));  
		}
    m_ownpRecordset->Update();
   m_ownpRecordset->Close();
    m_ownpRecordset = NULL;
 pCon_ex.Release();

 }
  catch(_com_error *e)

  {
   AfxMessageBox(e->ErrorMessage());
  }
CoUninitialize();
  }

// 查询道路表中所有字段
pCon_ex.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch (_ com_error * e)

{
AfxMessageBox(e-> ErrorMessage());
}
try
{

m_ownpRecordset-> MoveFirst();
int max_count = 20 ;
for int i = 0 ; i< max_count; i ++)
{
m_ownpRecordset-> AddNew();
CString astr = BDialog-> m_StandrdList.GetItemText(i, 0 );
CString bstr = BDialog-> m_StandrdList.GetItemText(i, 1 );
CString estr = BDialog-> m_StandrdList.GetItemText(i, 3 );
CString fstr = BDialog-> m_StandrdList.GetItemText(i, 4 );

_variant_t t = _variant_t( long 0 ));
m_ownpRecordset-> PutCollect(& t,_variant_t(astr));
t = _variant_t( long 1 ));
m_ownpRecordset-> PutCollect(& t,_variant_t(bstr));
t = _variant_t( long 4 ));
m_ownpRecordset-> PutCollect(& t,_variant_t(estr));
t = _variant_t( long 5 ));
m_ownpRecordset-> PutCollect(& t,_variant_t(fstr));
}
m_ownpRecordset-> Update();
m_ownpRecordset-> Close();
m_ownpRecordset = NULL;
pCon_ex.Release();

}
catch (_ com_error * e)

{
AfxMessageBox(e- &GT;的ErrorMessage());
}
CoUninitialize();
}
",// 查询道路表中所有字段 pCon_ex.GetInterfacePtr(), // 获取库接库的IDispatch指针 adOpenDynamic, adLockOptimistic, adCmdText); } catch(_com_error *e) { AfxMessageBox(e->ErrorMessage()); } try { m_ownpRecordset->MoveFirst(); int max_count=20; for(int i=0;i< max_count;i++) { m_ownpRecordset->AddNew(); CString astr=BDialog->m_StandrdList.GetItemText(i,0); CString bstr=BDialog->m_StandrdList.GetItemText(i,1); CString estr=BDialog->m_StandrdList.GetItemText(i,3); CString fstr=BDialog->m_StandrdList.GetItemText(i,4); _variant_t t = _variant_t(long(0)); m_ownpRecordset->PutCollect(&t,_variant_t(astr)); t = _variant_t(long(1)); m_ownpRecordset->PutCollect(&t,_variant_t(bstr)); t = _variant_t(long(4)); m_ownpRecordset->PutCollect(&t,_variant_t(estr)); t = _variant_t(long(5)); m_ownpRecordset->PutCollect(&t,_variant_t(fstr)); } m_ownpRecordset->Update(); m_ownpRecordset->Close(); m_ownpRecordset = NULL; pCon_ex.Release(); } catch(_com_error *e) { AfxMessageBox(e->ErrorMessage()); } CoUninitialize(); }


确保编译目标设置为x86(32位应用程序)。 32位应用程序也可以在64位系统上运行。另外,在Win7-64上使用32位Excel。
Make sure the compilation target is set to x86 (32bit application). 32bit applications can also run on 64bit systems. Also, use 32bit Excel on Win7-64.


JET.OLEDB.4.0驱动程序没有64位支持。如果你想使用它,请遵循Bernhard的解决方案。



可选择使用64位ACE.OLEDB.12.0驱动程序,它可以与64位版本一起安装'Microsoft Access数据库引擎2010可再发行'。
The JET.OLEDB.4.0 driver has no 64 bit support. If you want to use it, follow Bernhard's solution.

Optionally use the 64-bit ACE.OLEDB.12.0 driver which can be installed with the 64-bit version of the 'Microsoft Access Database Engine 2010 Redistributable'.