C语言 Microsoft Office 提供COM接口 使用实例
时间: 2024-02-05 20:02:53 浏览: 159
下面是一个使用C语言调用Microsoft Excel的COM接口的实例:
```c
#include <windows.h>
#include <objbase.h>
#include <stdio.h>
#include <conio.h>
// 定义Excel应用程序和工作簿对象
IDispatch *pExcelApp = NULL, *pWorkbook = NULL;
// 定义Excel工作表对象
IDispatch *pWorksheet = NULL;
int main()
{
HRESULT hr;
CLSID clsid;
DISPID dispid;
VARIANT result;
DISPPARAMS params;
EXCEPINFO excepInfo;
BSTR bstr;
// 初始化COM库
CoInitialize(NULL);
// 创建Excel应用程序对象
hr = CLSIDFromProgID(L"Excel.Application", &clsid);
hr = CoCreateInstance(&clsid, NULL, CLSCTX_LOCAL_SERVER, &IID_IDispatch, (void **)&pExcelApp);
// 显示Excel应用程序窗口
params.cArgs = 1;
params.rgvarg = (VARIANTARG*)malloc(sizeof(VARIANTARG) * params.cArgs);
params.rgvarg[0].vt = VT_BOOL;
params.rgvarg[0].boolVal = TRUE;
params.cNamedArgs = 1;
params.rgdispidNamedArgs = (DISPID*)malloc(sizeof(DISPID) * params.cNamedArgs);
bstr = SysAllocString(L"Visible");
hr = pExcelApp->GetIDsOfNames(IID_NULL, &bstr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
params.rgdispidNamedArgs[0] = dispid;
hr = pExcelApp->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYPUT, ¶ms, NULL, &excepInfo, NULL);
// 打开工作簿
params.cArgs = 1;
params.rgvarg = (VARIANTARG*)malloc(sizeof(VARIANTARG) * params.cArgs);
params.rgvarg[0].vt = VT_BSTR;
params.rgvarg[0].bstrVal = SysAllocString(L"C:\\example.xlsx");
params.cNamedArgs = 1;
params.rgdispidNamedArgs = (DISPID*)malloc(sizeof(DISPID) * params.cNamedArgs);
bstr = SysAllocString(L"FileName");
hr = pExcelApp->GetIDsOfNames(IID_NULL, &bstr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
params.rgdispidNamedArgs[0] = dispid;
hr = pExcelApp->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, ¶ms, NULL, &excepInfo, NULL);
hr = pExcelApp->GetIDsOfNames(IID_NULL, &bstr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
hr = pExcelApp->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, ¶ms, &result, &excepInfo, NULL);
pWorkbook = result.pdispVal;
// 获取工作表对象
params.cArgs = 1;
params.rgvarg = (VARIANTARG*)malloc(sizeof(VARIANTARG) * params.cArgs);
params.rgvarg[0].vt = VT_I4;
params.rgvarg[0].lVal = 1;
params.cNamedArgs = 1;
params.rgdispidNamedArgs = (DISPID*)malloc(sizeof(DISPID) * params.cNamedArgs);
bstr = SysAllocString(L"Item");
hr = pWorkbook->GetIDsOfNames(IID_NULL, &bstr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
params.rgdispidNamedArgs[0] = dispid;
hr = pWorkbook->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, ¶ms, &result, &excepInfo, NULL);
pWorksheet = result.pdispVal;
// 修改单元格值
params.cArgs = 2;
params.rgvarg = (VARIANTARG*)malloc(sizeof(VARIANTARG) * params.cArgs);
params.rgvarg[1].vt = VT_I4;
params.rgvarg[1].lVal = 1;
params.rgvarg[0].vt = VT_I4;
params.rgvarg[0].lVal = 1;
params.cNamedArgs = 0;
bstr = SysAllocString(L"Cells");
hr = pWorksheet->GetIDsOfNames(IID_NULL, &bstr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
hr = pWorksheet->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, ¶ms, &result, &excepInfo, NULL);
IDispatch *pCell = result.pdispVal;
params.cArgs = 1;
params.rgvarg = (VARIANTARG*)malloc(sizeof(VARIANTARG) * params.cArgs);
params.rgvarg[0].vt = VT_BSTR;
params.rgvarg[0].bstrVal = SysAllocString(L"Hello World!");
params.cNamedArgs = 1;
params.rgdispidNamedArgs = (DISPID*)malloc(sizeof(DISPID) * params.cNamedArgs);
bstr = SysAllocString(L"Value");
hr = pCell->GetIDsOfNames(IID_NULL, &bstr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
params.rgdispidNamedArgs[0] = dispid;
hr = pCell->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYPUT, ¶ms, NULL, &excepInfo, NULL);
// 保存工作簿并关闭Excel应用程序
params.cArgs = 0;
params.cNamedArgs = 0;
bstr = SysAllocString(L"Save");
hr = pWorkbook->GetIDsOfNames(IID_NULL, &bstr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
hr = pWorkbook->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, ¶ms, NULL, &excepInfo, NULL);
bstr = SysAllocString(L"Close");
hr = pWorkbook->GetIDsOfNames(IID_NULL, &bstr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
hr = pWorkbook->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, ¶ms, NULL, &excepInfo, NULL);
bstr = SysAllocString(L"Quit");
hr = pExcelApp->GetIDsOfNames(IID_NULL, &bstr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
hr = pExcelApp->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, ¶ms, NULL, &excepInfo, NULL);
// 释放COM对象和内存
pExcelApp->Release();
pWorkbook->Release();
pWorksheet->Release();
pCell->Release();
SysFreeString(bstr);
CoUninitialize();
return 0;
}
```
这个例子演示了如何创建Excel应用程序对象并打开一个工作簿,然后修改第一个单元格的值,并保存工作簿并关闭Excel应用程序。
阅读全文