1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
// g++ Main.cpp XLFunctions.cpp -oXL19.exe -m64 -lole32 -loleaut32 -luuid -Os -s
// cl Main.cpp XLFunctions.cpp /O1 /Os /FeXL19_VC.exe kernel32.lib ole32.lib oleaut32.lib uuid.lib
// cl Main.cpp XLFunctions.cpp /O1 /Os /FeXL19_TCLib.exe /GS- TCLib.lib kernel32.lib ole32.lib oleaut32.lib uuid.lib
// 7,680 Bytes VC15; x64; TCLib Linkage /nodefaultlib:libcmt.lib
// 20,480 Bytes Mingw 4.8; x64. (Links with msvcrt.dll)
// 79,872 Bytes VC15; x64; C Std. Lib. Linkage
//#define TCLib
#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
#include <windows.h>
#ifdef TCLib
#include "string.h"
#include "stdio.h"
extern "C" int _fltused = 1;
#else
#include <stdio.h>
#include <string.h>
#endif
#include "XLFunctions.h"
int main()
{
const CLSID CLSID_XLApplication = {0x00024500,0x0000,0x0000,{0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
const IID IID_Application = {0x000208D5,0x0000,0x0000,{0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46}};
IDispatch* pXLApp = NULL;
IDispatch* pXLWorkbooks = NULL;
IDispatch* pXLWorkbook = NULL;
IDispatch* pXLWorksheets = NULL;
IDispatch* pXLWorksheet = NULL;
DISPPARAMS NoArgs = {NULL,NULL,0,0};
wchar_t szWorkBookPath[] = L"C:\\Code\\CodeBlks\\XL_Dispatch\\XL19\\Book1.xls"; // << Fix For Your Path!!!
wchar_t szSheet[] = L"Sheet1";
SYSTEMTIME st;
wchar_t szNumber[4];
wchar_t szRange[4];
wchar_t szCell[64];
wchar_t szMonth[4];
wchar_t szDay[4];
wchar_t szYear[8];
wchar_t szDate[16];
VARIANT vReturn;
HRESULT hr;
LCID lcid;
CoInitialize(NULL);
hr=CoCreateInstance(CLSID_XLApplication, NULL, CLSCTX_LOCAL_SERVER, IID_Application, (void**)&pXLApp); // Returns in last [out] parameter pointer to app object
if(SUCCEEDED(hr))
{
lcid=GetUserDefaultLCID();
SetVisible(pXLApp,lcid);
pXLWorkbooks=GetDispatchObject(pXLApp,572,DISPATCH_PROPERTYGET,lcid);
if(pXLWorkbooks)
{
pXLWorkbook=OpenXLWorkBook(pXLWorkbooks,lcid,szWorkBookPath);
if(pXLWorkbook)
{
pXLWorksheets=GetDispatchObject(pXLWorkbook,494,DISPATCH_PROPERTYGET,lcid);
if(pXLWorksheets)
{
pXLWorksheet=SelectWorkSheet(pXLWorksheets,lcid,szSheet);
if(pXLWorksheet)
{
for(size_t i=0; i<4; i++) // GetCell() and GetXLCell() are the two functions from XLFunctions.cpp
{ // that retrieve *.xls data. Their use is fairly simple. Basically,
szRange[0]=65+i; // you just pass in an IDispatch* to the Excel SpreadSheet previously
szRange[1]=L'\0'; // obtained, and a character string such as A1, B2, C3, etc, for the cell
swprintf(szNumber,L"%u",1); // for which you want the cell contents retrieved. The only thing a bit
wcscat(szRange,szNumber); // wierd is the data is returned in a VARIANT in the last parameter of the
GetXLCell(pXLWorksheet,lcid,szRange,szCell,64); // function call in GetCell(). In GetXLCell() the cell contents in string
wprintf(L"%s\t",szCell); // form is returned in the next to last parameter, which is an [out] para-
} // meter.
printf("\n");
for(size_t i=2; i<=5; i++) // data in rows 2 - 5 // Most data is returned as a C/C++ double (VARIANT.vt==VT_R8), a Windows
{ // BSTR (VARIANT.vt==VT_BSTR), or a Windows date (VARIANT.vt==VT_DATE); and
for(size_t j=65; j<=68; j++) // 65=A, 66=B, etc. // a VT_DATE is actually a double. In many instances you need to test the
{ // VARIANT.vt to find out in what form the data was returned to you, because
szRange[0]=j; // A1, B1, C1, D1, etc. // the user can right click on a cell and change the formatting.
szRange[1]=L'\0'; // A2, B2, C2, D2, etc.
swprintf(szNumber,L"%u",i); // Note that VariantClear() should be called after each access to prevent
wcscat(szRange,szNumber); // memory leaks. In this app its not an issue but its something to keep
switch(j) // in mind. Ask if you want further info.
{
case 65: // 'A' // Note I removed error checking on all my calls to GetCell() and GetXLCell().
hr=GetCell(pXLWorksheet,lcid,szRange,vReturn); // You probably shouldn't do this in a real app. They return an HRESULT
printf("%-4.0f\t",vReturn.dblVal); // and I'd recommend you always test this with the SUCCEEDED(hr) macro.
VariantClear(&vReturn);
break;
case 66: // 'B'
GetCell(pXLWorksheet,lcid,szRange,vReturn);
printf("%10.4f\t",vReturn.dblVal);
VariantClear(&vReturn);
break;
case 67: // 'C'
GetCell(pXLWorksheet,lcid,szRange,vReturn);
memset(&st,0,sizeof(st));
VariantTimeToSystemTime(vReturn.dblVal,&st);
swprintf(szMonth,L"%d",st.wMonth);
swprintf(szDay,L"%d",st.wDay);
swprintf(szYear,L"%d",st.wYear);
wcscpy(szDate,szMonth);
wcscat(szDate,L"/");
wcscat(szDate,szDay);
wcscat(szDate,L"/");
wcscat(szDate,szYear);
wprintf(L"%s\t",szDate);
VariantClear(&vReturn);
break;
case 68: // 'D'
GetXLCell(pXLWorksheet,lcid,szRange,szCell,64);
wprintf(L"%s\t",szCell);
VariantClear(&vReturn);
break;
}
}
printf("\n");
}
pXLWorksheet->Release();
}
pXLWorksheets->Release();
}
pXLWorkbook->Release();
}
pXLWorkbooks->Release();
}
getchar();
pXLApp->Invoke(0x0000012e,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_METHOD,&NoArgs,NULL,NULL,NULL); // pXLApp->Quit() 0x12E
pXLApp->Release();
}
CoUninitialize();
return 0;
}
/*
Id Float_Point Date_Field Text_Field
1 3.1416 11/15/1952 My Birthday
2 1.2346 6/30/1969 Walk On Moon?
3 15.1234 1/1/2006 Some String
4 0.5432 4/1/2006 April Fools Day!
*/
|