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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
#include <windows.h>
#include <cstdio>
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}};
HRESULT GetXLCell(IDispatch* pXLWorksheet, LCID& lcid, wchar_t* pszRange, wchar_t* pszCell, size_t iBufferLength)
{
DISPPARAMS NoArgs = {NULL,NULL,0,0};
IDispatch* pXLRange = NULL;
VARIANT vArgArray[1];
VARIANT vResult;
DISPPARAMS DispParams;
HRESULT hr;
VariantInit(&vResult);
vArgArray[0].vt = VT_BSTR,
vArgArray[0].bstrVal = SysAllocString(pszRange);
DispParams.rgvarg = vArgArray;
DispParams.rgdispidNamedArgs = 0;
DispParams.cArgs = 1; // Try to get Range
DispParams.cNamedArgs = 0; // Invoke _Worksheet::Range("A1") << returns IDispatch** to dispinterface Range
// [id(0x000000c5), propget, helpcontext(0x000100c5)] HRESULT Range([in] VARIANT Cell1, [in, optional] VARIANT Cell2, [out, retval] Range** RHS);
hr=pXLWorksheet->Invoke(0xC5,IID_NULL,lcid,DISPATCH_PROPERTYGET,&DispParams,&vResult,NULL,NULL);
if(FAILED(hr))
return E_FAIL;
pXLRange=vResult.pdispVal;
//Member Get Value <6> () As Variant
VariantClear(&vArgArray[0]);
hr=pXLRange->Invoke(6,IID_NULL,lcid,DISPATCH_PROPERTYGET,&NoArgs,&vResult,NULL,NULL);
if(SUCCEEDED(hr))
{
if(vResult.vt==VT_BSTR)
{
if(SysStringLen(vResult.bstrVal)<iBufferLength)
{
wcscpy(pszCell,vResult.bstrVal);
return S_OK;
}
}
else
pszCell[0]=0;
VariantClear(&vResult);
}
pXLRange->Release();
return E_FAIL;
}
HRESULT GetCell(IDispatch* pXLSheet, LCID lcid, wchar_t* pszRange, VARIANT& pVt)
{
DISPPARAMS NoArgs = {NULL,NULL,0,0};
IDispatch* pXLRange = NULL;
VARIANT vArgArray[1];
VARIANT vResult;
DISPPARAMS DispParams;
HRESULT hr;
VariantInit(&vResult);
vArgArray[0].vt = VT_BSTR,
vArgArray[0].bstrVal = SysAllocString(pszRange);
DispParams.rgvarg = vArgArray;
DispParams.rgdispidNamedArgs = 0;
DispParams.cArgs = 1; // Try to get Range
DispParams.cNamedArgs = 0; // Invoke _Worksheet::Range("A1") << returns IDispatch** to dispinterface Range
// [id(0x000000c5), propget, helpcontext(0x000100c5)] HRESULT Range([in] VARIANT Cell1, [in, optional] VARIANT Cell2, [out, retval] Range** RHS);
hr=pXLSheet->Invoke(0xC5,IID_NULL,lcid,DISPATCH_PROPERTYGET,&DispParams,&vResult,NULL,NULL);
if(FAILED(hr))
return hr;
pXLRange=vResult.pdispVal;
//Member Get Value <6> () As Variant
VariantClear(&vArgArray[0]);
VariantClear(&pVt);
hr=pXLRange->Invoke(6,IID_NULL,lcid,DISPATCH_PROPERTYGET,&NoArgs,&pVt,NULL,NULL);
pXLRange->Release();
return hr;
}
IDispatch* SelectWorkSheet(IDispatch* pXLWorksheets, LCID& lcid, wchar_t* pszSheet)
{
VARIANT vResult;
HRESULT hr;
VARIANT vArgArray[1];
DISPPARAMS DispParams;
DISPID dispidNamed;
IDispatch* pXLWorksheet = NULL;
// Member Get Item <170> (In Index As Variant<0>) As IDispatch >> Gets pXLWorksheet
// [id(0x000000aa), propget, helpcontext(0x000100aa)] IDispatch* Item([in] VARIANT Index);
VariantInit(&vResult);
vArgArray[0].vt = VT_BSTR;
vArgArray[0].bstrVal = SysAllocString(pszSheet);
DispParams.rgvarg = vArgArray;
DispParams.rgdispidNamedArgs = &dispidNamed;
DispParams.cArgs = 1;
DispParams.cNamedArgs = 0;
hr=pXLWorksheets->Invoke(0xAA,IID_NULL,lcid,DISPATCH_PROPERTYGET,&DispParams,&vResult,NULL,NULL);
if(FAILED(hr))
return NULL;
pXLWorksheet=vResult.pdispVal;
SysFreeString(vArgArray[0].bstrVal);
// Worksheet::Select()
VariantInit(&vResult);
VARIANT varReplace;
varReplace.vt = VT_BOOL;
varReplace.boolVal = VARIANT_TRUE;
dispidNamed = 0;
DispParams.rgvarg = &varReplace;
DispParams.rgdispidNamedArgs = &dispidNamed;
DispParams.cArgs = 1;
DispParams.cNamedArgs = 1;
hr=pXLWorksheet->Invoke(0xEB,IID_NULL,lcid,DISPATCH_METHOD,&DispParams,&vResult,NULL,NULL);
return pXLWorksheet;
}
IDispatch* OpenXLWorkBook(IDispatch* pXLWorkbooks, LCID& lcid, wchar_t* pszWorkBookPath)
{
VARIANT vResult;
VARIANT vArgArray[1];
DISPPARAMS DispParams;
DISPID dispidNamed;
HRESULT hr;
VariantInit(&vResult); // Call Workbooks::Open() - 682 >> Gets pXLWorkbook
vArgArray[0].vt = VT_BSTR;
vArgArray[0].bstrVal = SysAllocString(pszWorkBookPath);
DispParams.rgvarg = vArgArray;
DispParams.rgdispidNamedArgs = &dispidNamed;
DispParams.cArgs = 1;
DispParams.cNamedArgs = 0;
hr=pXLWorkbooks->Invoke(682,IID_NULL,lcid,DISPATCH_METHOD,&DispParams,&vResult,NULL,NULL);
SysFreeString(vArgArray[0].bstrVal);
if(FAILED(hr))
return NULL;
else
return vResult.pdispVal;
}
int main()
{
IDispatch* pXLApp = NULL;
IDispatch* pXLWorkbooks = NULL;
IDispatch* pXLWorkbook = NULL;
IDispatch* pXLWorksheets = NULL;
IDispatch* pXLWorksheet = NULL;
wchar_t szWorkBookPath[] = L"C:\\Code\\CodeBlks\\XL_Dispatch\\XL16\\Book1.xls"; // << Fix This For Your Path!!!
wchar_t szSheet[] = L"Sheet2";
wchar_t szRange[] = L"A1";
wchar_t szCell[64];
DISPPARAMS NoArgs = {NULL,NULL,0,0};
VARIANT vResult;
HRESULT hr;
LCID lcid;
CoInitialize(NULL);
hr=CoCreateInstance(CLSID_XLApplication, NULL, CLSCTX_LOCAL_SERVER, IID_Application, (void**)&pXLApp);
if(SUCCEEDED(hr))
{
printf("pXLApp = %p\n",pXLApp);
lcid=GetUserDefaultLCID();
VariantInit(&vResult); //572 Get _Application::Workbooks >> Gets pXLWorkbooks
hr=pXLApp->Invoke(572,IID_NULL,lcid,DISPATCH_PROPERTYGET,&NoArgs,&vResult,NULL,NULL);
if(SUCCEEDED(hr))
{
pXLWorkbooks=vResult.pdispVal;
printf("pXLWorkbooks = %p\n",pXLWorkbooks);
pXLWorkbook=OpenXLWorkBook(pXLWorkbooks,lcid,szWorkBookPath);
if(pXLWorkbook)
{
printf("pXLWorkbook = %p\n",pXLWorkbook);
VariantInit(&vResult);
hr=pXLWorkbook->Invoke(494,IID_NULL,lcid,DISPATCH_PROPERTYGET,&NoArgs,&vResult,NULL,NULL);
if(SUCCEEDED(hr))
{
pXLWorksheets=vResult.pdispVal;
printf("pXLWorksheets = %p\n",pXLWorksheets);
pXLWorksheet=SelectWorkSheet(pXLWorksheets,lcid,szSheet);
if(pXLWorksheet)
{
printf("pXLWorksheet = %p\n",pXLWorksheet);
GetXLCell(pXLWorksheet,lcid,szRange,szCell,64);
wprintf(L"szCell = %s\n",szCell);
pXLWorksheet->Release();
}
pXLWorksheets->Release();
}
pXLWorkbook->Release();
}
pXLWorkbooks->Release();
}
pXLApp->Invoke(0x0000012e,IID_NULL,LOCALE_USER_DEFAULT,DISPATCH_METHOD,&NoArgs,NULL,NULL,NULL); // pXLApp->Quit() 0x12E
pXLApp->Release();
}
CoUninitialize();
getchar();
return 0;
}
|