C语言 GetIDsOfNames 获取Range Excel表格实例
时间: 2024-02-02 14:02:25 浏览: 70
首先,你需要使用 Microsoft Excel 的 COM 组件来打开 Excel 应用程序,并获取 Range 对象。
以下是一个简单的 C 代码示例,用于获取 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;
// 初始化 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 = InvokeMethod(pExcelApp, L"Workbooks", &vtWorkbooks, 1, &vtWorkbook);
if (FAILED(hr))
{
printf("InvokeMethod Workbooks failed: %x\n", hr);
pExcelApp->Release();
CoUninitialize();
return 1;
}
hr = InvokeMethod(pWorkbook, L"Open", L"C:\\test.xlsx", NULL, 0);
if (FAILED(hr))
{
printf("InvokeMethod Open failed: %x\n", hr);
pWorkbook->Release();
pExcelApp->Release();
CoUninitialize();
return 1;
}
// 获取工作表对象
vtWorksheets.vt = VT_DISPATCH;
vtWorksheets.pdispVal = pWorkbook;
hr = InvokeMethod(pWorkbook, L"Worksheets", &vtWorksheets, 1, &vtWorksheet);
if (FAILED(hr))
{
printf("InvokeMethod Worksheets failed: %x\n", hr);
pWorkbook->Release();
pExcelApp->Release();
CoUninitialize();
return 1;
}
// 获取 Range 对象
vtRange.vt = VT_BSTR;
vtRange.bstrVal = SysAllocString(L"A1:B2");
hr = InvokeMethod(pWorksheet, L"Range", &vtRange, 1, &vtRange);
if (FAILED(hr))
{
printf("InvokeMethod Range failed: %x\n", hr);
pWorksheet->Release();
pWorkbook->Release();
pExcelApp->Release();
CoUninitialize();
return 1;
}
// TODO: 使用 Range 对象进行操作
// 释放所有对象
VariantClear(&vtRange);
pRange->Release();
VariantClear(&vtWorksheet);
pWorksheet->Release();
VariantClear(&vtWorksheets);
pWorkbook->Release();
VariantClear(&vtWorkbook);
pExcelApp->Release();
CoUninitialize();
return 0;
}
// InvokeMethod 函数用于调用 COM 对象的方法
HRESULT InvokeMethod(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;
}
```
上述代码中的 `InvokeMethod` 函数用于调用 COM 对象的方法,其中 `pObj` 参数为要调用的对象,`szName` 参数为方法名,`pvResult` 参数为返回值,`cArgs` 参数为参数个数,后面的可变参数为具体的参数。
阅读全文