Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C++
//#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();
  }
Posted
Updated 26-May-13 2:27am
v4
Comments
Richard MacCutchan 26-May-13 8:27am    
No idea; please provide some proper diagnostic information.
zhouxman 26-May-13 8:40am    
There have no diagnostic information,the code can succeful run at win7(32bit) and add new record into the excel file. and compile and link succefull at win7(64bit),but can not add new record.

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.
 
Share this answer
 
Comments
zhouxman 27-May-13 20:16pm    
Thanks ,My a application is a dll mode,and Plug-in application, the main application is 64 bit. so my compilation target is set to x64 (64bit application). the Excel file was edit on win7(32bit).how to?
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'.
 
Share this answer
 
Comments
zhouxman 27-May-13 4:34am    
thanks how to write the " ConnectionString " please give me a point.
I must acess the excel file.
Jochen Arndt 27-May-13 4:53am    
The connection string is build the same way as with the JET driver:
Provider: Microsoft.ACE.OLEDB.12.0
Extended Properties: "Excel 12.0", "Excel 12.0 Xml", "Excel 12.0 Macro" for XLSB / XLSX / XLSM files.

See also http://www.connectionstrings.com/excel#ace-oledb-12-0
zhouxman 27-May-13 6:17am    
Thanks very much, I will try it as soon,and give you the news.
zhouxman 27-May-13 20:21pm    
Thank,My a application is a dll mode,and Plug-in application, the main application is 64 bit. so my compilation target is set to x64 (64bit application). the Excel file was edit on win7(32bit)
and if the MSoffice is 2010 ver. this string is worked?

So my connection string is follow this(form the url you give).but the data also can't write in the excel file,
///////////////////////////////////////
Excel 97-2003 Xls files with ACE OLEDB 12.0
You can use this connection string to use the Office 2007 OLEDB driver (ACE 12.0) to connect to older 97-2003 Excel workbooks.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myOldExcelFile.xls;
Extended Properties="Excel 8.0;HDR=YES";
///////////////////////////////////////
Jochen Arndt 28-May-13 3:01am    
If you want to write, you must keep the "IMEX=0" extended property. I have used the ACE.12 provider for reading and writing, but with 32-bit apps and driver only.
Did you get any errors (exceptions)?
Permalink is right,everything is OK!
Thanks very one!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900