Excel Automation

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
My application should write data into template excel file and save it under a new name in excel format. Problem is, it will be used on PCs with different versions of Excel (2000 - 2010). What is the procedure? Is it possible to do?

First application was builded in MFC, and now is imported to C++ Microsoft Visual Studio 2010.

Next code (old one) is depend of the instaled Excel version ...

// Microsoft Office 2000<br/>
#import "C:Program FilesMicrosoft OfficeOfficeMSO9.DLL"<br/>
#import "C:Program FilesCommon FilesMicrosoft SharedVBAVBA6VBE6EXT.OLB"<br/>
#import "C:Program FilesMicrosoft OfficeOfficeEXCEL9.OLB" rename("DialogBox","_DialogBox") rename("RGB","_RGB") exclude("IFont","IPicture")
HRESULT hr = ::OleInitialize(NULL); <br/>
_ASSERT(SUCCEEDED(hr)); <br/>

CString strTmpl = _T("templete.xlt");
Excel::_WorkbookPtr ptrWrkBook = ptrExclApp->Workbooks->Open(strTmpl.AllocSysString()); <br/>
Excel::_WorksheetPtr data = ptrWrkBook->Sheets->Item[1];<br/>
<br/>
GetDlgItemText(IDC_ACT_SN, str);<br/>
COleVariant oleVar(str);<br/>
data->Cells->Item[7][6]=oleVar;
...
COleVariant cesta(fileName);<br/>
ptrWrkBook->SaveAs(cesta, Excel::XlFileFormat::xlExcel9795, "", "", false, false, Excel::XlSaveAsAccessMode::xlExclusive );<br/>

ptrExclApp->Quit();
::OleUninitialize();

Thanks ...


View the full article
 
Back
Top