C语言 GetIDsOfNames 获取Range Excel表格使用实例
时间: 2023-08-21 21:05:00 浏览: 173
以下是一个简单的 C 代码示例,演示如何使用 `GetIDsOfNames` 和 `Invoke` 方法来获取 Excel 表格中的 Range 对象,并设置单元格的值。
```c
#include <stdio.h>
#include <windows.h>
#include "ole2.h"
#include "oleauto.h"
int main()
{
HRESULT hr;
CLSID clsid;
IDispatch *pExcelApp = NULL, *pWorkbook = NULL, *pWorksheet = NULL, *pRange = NULL;
VARIANT vtWorkbooks, vtWorkbook, vtWorksheets, vtWorksheet, vtRange, vtValue;
// 初始化 COM 库
hr = CoInitialize(NULL);
if (FAILED(hr))
{
printf("CoInitialize failed: %x\n", hr);
return 1;
}
// 获取 Excel 应用程序对象
hr = CLSIDFromProgID(L"Excel.Application", &clsid);
if (FAILED(hr))
{
printf("CLSIDFromProgID failed: %x\n", hr);
CoUninitialize();
return 1;
}
hr = CoCreateInstance(&clsid, NULL, CLSCTX_LOCAL_SERVER, &IID_IDispatch, (void **)&pExcelApp);
if (FAILED(hr))
{
printf("CoCreateInstance failed: %x\n", hr);
CoUninitialize();
return 1;
}
// 打开工作簿
vtWorkbooks.vt = VT_DISPATCH;
vtWorkbooks.pdispVal = pExcelApp;
hr = Invoke(pExcelApp, L"Workbooks", &vtWorkbooks, 1, &vtWorkbook);
if (FAILED(hr))
{
printf("Invoke Workbooks failed: %x\n", hr);
pExcelApp->Release();
CoUninitialize();
return 1;
}
hr = Invoke(pWorkbook, L"Open", L"C:\\test.xlsx", NULL, 0);
if (FAILED(hr))
{
printf("Invoke Open failed: %x\n", hr);
pWorkbook->Release();
pExcelApp->Release();
CoUninitialize();
return 1;
}
// 获取工作表对象
vtWorksheets.vt = VT_DISPATCH;
vtWorksheets.pdispVal = pWorkbook;
hr = Invoke(pWorkbook, L"Worksheets", &vtWorksheets, 1, &vtWorksheet);
if (FAILED(hr))
{
printf("Invoke Worksheets failed: %x\n", hr);
pWorkbook->Release();
pExcelApp->Release();
CoUninitialize();
return 1;
}
// 获取 Range 对象
vtRange.vt = VT_BSTR;
vtRange.bstrVal = SysAllocString(L"A1:B2");
hr = Invoke(pWorksheet, L"Range", &vtRange, 1, &vtRange);
if (FAILED(hr))
{
printf("Invoke Range failed: %x\n", hr);
pWorksheet->Release();
pWorkbook->Release();
pExcelApp->Release();
CoUninitialize();
return 1;
}
// 设置单元格的值
vtValue.vt = VT_BSTR;
vtValue.bstrVal = SysAllocString(L"Hello World!");
hr = SetProperty(pRange, L"Value", &vtValue);
if (FAILED(hr))
{
printf("SetProperty Value failed: %x\n", hr);
pRange->Release();
pWorksheet->Release();
pWorkbook->Release();
pExcelApp->Release();
CoUninitialize();
return 1;
}
// 释放所有对象
VariantClear(&vtValue);
VariantClear(&vtRange);
pRange->Release();
VariantClear(&vtWorksheet);
pWorksheet->Release();
VariantClear(&vtWorksheets);
pWorkbook->Release();
VariantClear(&vtWorkbook);
pExcelApp->Release();
CoUninitialize();
return 0;
}
// Invoke 函数用于调用 COM 对象的方法
HRESULT Invoke(IDispatch *pObj, LPCWSTR szName, VARIANT *pvResult, int cArgs, ...)
{
HRESULT hr;
DISPID dispid;
DISPPARAMS dispparams = {0};
VARIANTARG *pArgs = NULL;
EXCEPINFO excepinfo;
// 获取方法的 DISPID
hr = pObj->GetIDsOfNames(IID_NULL, (LPOLESTR *)&szName, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
if (FAILED(hr))
{
printf("GetIDsOfNames failed: %x\n", hr);
return hr;
}
if (cArgs > 0)
{
pArgs = new VARIANTARG[cArgs];
va_list marker;
va_start(marker, cArgs);
for (int i = 0; i < cArgs; i++)
{
pArgs[i] = va_arg(marker, VARIANTARG);
}
va_end(marker);
dispparams.cArgs = cArgs;
dispparams.rgvarg = pArgs;
}
// 调用方法
hr = pObj->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &dispparams, pvResult, &excepinfo, NULL);
if (FAILED(hr))
{
printf("Invoke %S failed: %x\n", szName, hr);
return hr;
}
if (pArgs != NULL)
{
delete[] pArgs;
}
return S_OK;
}
// SetProperty 函数用于设置 COM 对象的属性值
HRESULT SetProperty(IDispatch *pObj, LPCWSTR szName, VARIANT *pvValue)
{
HRESULT hr;
DISPID dispid;
DISPID namedArgs[] = {DISPID_PROPERTYPUT};
DISPPARAMS dispparams = {0};
VARIANTARG rgvarg[2];
EXCEPINFO excepinfo;
// 获取属性的 DISPID
hr = pObj->GetIDsOfNames(IID_NULL, (LPOLESTR *)&szName, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
if (FAILED(hr))
{
printf("GetIDsOfNames failed: %x\n", hr);
return hr;
}
// 设置属性值
rgvarg[1] = *pvValue;
rgvarg[0].vt = VT_EMPTY;
dispparams.cArgs = 1;
dispparams.rgvarg = rgvarg;
dispparams.cNamedArgs = 1;
dispparams.rgdispidNamedArgs = namedArgs;
hr = pObj->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYPUT, &dispparams, NULL, &excepinfo, NULL);
if (FAILED(hr))
{
printf("Invoke %S failed: %x\n", szName, hr);
return hr;
}
return S_OK;
}
```
上述代码中的 `Invoke` 函数用于调用 COM 对象的方法,其中 `pObj` 参数为要调用的对象,`szName` 参数为方法名,`pvResult` 参数为返回值,`cArgs` 参数为参数个数,后面的可变参数为具体的参数。`SetProperty` 函数用于设置 COM 对象的属性值,其中 `pObj` 参数为要设置属性的对象,`szName` 参数为属性名,`pvValue` 参数为属性值。
阅读全文