#include <ole2.h>
#include <stdio.h>
#pragma warning (disable:4146 4192 4786 4081)
#define USE_OFFICEXP 1
#ifdef USE_OFFICEXP
#import "C:/Program Files/Common Files/Microsoft Shared/Office10/MSO.DLL"
#import "c://Program Files//Common Files/Microsoft Shared/VBA/VBA6/VBE6EXT.olb"
#import "c:/Program Files/Microsoft Office/Office10/MSWORD.olb" rename("ExitWindows","ExitWindowsEx")
#import "c:/Program Files/Microsoft Office/Office10/excel.exe" rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") /
rename("DocumentProperties", "DocumentPropertiesXL") exclude("IFont","IPicture")
#else
#import "c:/Program Files/Microsoft Office/Office/MSO9.dll"
#import "C:/Program Files/Common Files/Microsoft Shared/VBA/VBA6/VBE6EXT.OLB" no_namespace
#import "c:/Program Files/Microsoft Office/Office/MSWORD9.OLB" rename("ExitWindows","_ExitWindows")
#import "c:/Program Files/Microsoft Office/Office/excel9.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/n",iColCnt);
int iRowCnt = pRange ->GetRows() ->GetCount();
printf("used rows is %d/n",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的问题。于是写了一下。写完了再保存。顺便发一下。这可是原创的哈。好想有三颗星星了。