#include <ole2.h>
#include <stdio.h>
#pragma warning (disable:4146 4192 4786 4081)
#define USE_OfficeXP 1
#ifdef USE_OfficeXP
#import "C:Program FilesCommon FilesMicrosoft SharedOffice10MSO.DLL"
#import "c:\Program Files\Common FilesMicrosoft SharedVBAVBA6VBE6EXT.olb"
#import "c:Program FilesMicrosoft OfficeOffice10MSWord.olb" rename("ExitWindows","ExitWindowsEx")
#import "c:Program FilesMicrosoft OfficeOffice10Excel.exe" rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL")
rename("DocumentProperties", "DocumentPropertIEsXL") exclude("IFont","IPicture")
#else
#import "c:Program FilesMicrosoft OfficeOfficeMSO9.dll"
#import "C:Program FilesCommon FilesMicrosoft SharedVBAVBA6VBE6EXT.OLB" no_namespace
#import "c:Program FilesMicrosoft OfficeOfficeMSWord9.OLB" rename("ExitWindows","_ExitWindows")
#import "c:Program FilesMicrosoft OfficeOfficeExcel9.olb" rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL")
rename("DocumentProperties", "DocumentPropertIEsXL") exclude("IFont","IPicture")
#endif
int main(int argc, char* argv[])
{
VARIANT varItem;
varItem.vt = VT_I4;
varItem.intVal = 1;
::CoInitialize(NULL);
CLSID clsid;
#if 0
CLSIDFromProgID(L"Word.Application", &clsid);
Word::_ApplicationPtr pApp;
pApp.CreateInstance (clsid,NULL);
pApp ->PutVisible (VARIANT_TRUE);
pApp ->GetDocuments() ->Add();
Word::_DocumentPtr pWordDocument =pApp-> GetActiveDocument();
Word::SelectionPtr pSelection = pApp ->GetSelection();
pSelection ->TypeText("badguy");
#endif
#if 1
CLSIDFromProgID(L"Excel.Application", &clsid);
Excel::_ApplicationPtr pApp;
pApp .CreateInstance (clsid,NULL);
pApp ->PutVisible (0,VARIANT_TRUE);
Excel::_WorkbookPtr pWorkbook = pApp ->GetWorkbooks () ->Add ();
Excel::_WorksheetPtr pSheet = pWorkbook ->GetWorksheets () ->GetItem (varItem);
Excel::RangePtr pRange = pSheet ->GetUsedRange(0);
int iColCnt = pRange ->GetColumns() ->GetCount();
printf("used columns is %d ",iColCnt);
int iRowCnt = pRange ->GetRows() ->GetCount();
printf("used rows is %d ",iRowCnt);
pRange = pSheet ->GetRange(OLESTR("A1"));
pRange ->Value2 = OLESTR("SOME");
Excel::InteriorPtr pInterior= pRange ->GetInterior ();
varItem.intVal = 0xff0000;
pInterior ->PutColor (varItem);
pSheet ->PrintPrevIEw ();
#endif
::CoUninitialize();
return 0;
}
好久沒回復貼子,今天看到一個Excel的問題。於是寫了一下。寫完了再保存。順便發一下。這可是原創的哈。好想有三顆星星了。