C语言 WIN32 API写入Excel实例源码
时间: 2023-10-14 07:04:16 浏览: 124
下面是一个基于WIN32 API的C语言写入Excel的实例源码:
```c
#include <windows.h>
#include <ole2.h>
void WriteExcel()
{
// 初始化COM库
CoInitialize(NULL);
// 创建Excel应用程序对象
IDispatch* pExcelApp = NULL;
CLSID clsid;
CLSIDFromProgID(L"Excel.Application", &clsid);
HRESULT hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void**)&pExcelApp);
if (FAILED(hr))
{
MessageBox(NULL, L"创建Excel失败!", L"错误", MB_OK);
return;
}
// 获取Workbooks对象
IDispatch* pWorkbooks = NULL;
VARIANT vtIndex;
vtIndex.vt = VT_I4;
vtIndex.lVal = 1;
hr = pExcelApp->Invoke(0x0000018d, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &vtIndex, &pWorkbooks, NULL, NULL);
if (FAILED(hr))
{
MessageBox(NULL, L"获取Workbooks失败!", L"错误", MB_OK);
pExcelApp->Release();
return;
}
// 添加一个新的Workbook
IDispatch* pWorkbook = NULL;
hr = pWorkbooks->Invoke(0x000001c0, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, NULL, &pWorkbook, NULL, NULL);
if (FAILED(hr))
{
MessageBox(NULL, L"添加Workbook失败!", L"错误", MB_OK);
pWorkbooks->Release();
pExcelApp->Release();
return;
}
// 获取Worksheets对象
IDispatch* pWorksheets = NULL;
hr = pWorkbook->Invoke(0x000001c5, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, NULL, &pWorksheets, NULL, NULL);
if (FAILED(hr))
{
MessageBox(NULL, L"获取Worksheets失败!", L"错误", MB_OK);
pWorkbook->Release();
pWorkbooks->Release();
pExcelApp->Release();
return;
}
// 获取第一个Worksheet
IDispatch* pWorksheet = NULL;
vtIndex.lVal = 1;
hr = pWorksheets->Invoke(0x000001c6, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &vtIndex, &pWorksheet, NULL, NULL);
if (FAILED(hr))
{
MessageBox(NULL, L"获取第一个Worksheet失败!", L"错误", MB_OK);
pWorksheets->Release();
pWorkbook->Release();
pWorkbooks->Release();
pExcelApp->Release();
return;
}
// 写入数据
VARIANT vtValue;
VariantInit(&vtValue);
vtValue.vt = VT_BSTR;
vtValue.bstrVal = SysAllocString(L"Hello, World!");
hr = pWorksheet->Invoke(0x0000006e, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYPUT, NULL, &vtValue, NULL, NULL);
if (FAILED(hr))
{
MessageBox(NULL, L"写入数据失败!", L"错误", MB_OK);
pWorksheet->Release();
pWorksheets->Release();
pWorkbook->Release();
pWorkbooks->Release();
pExcelApp->Release();
return;
}
// 保存Workbook
hr = pWorkbook->Invoke(0x000000fd, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, NULL, NULL, NULL, NULL);
if (FAILED(hr))
{
MessageBox(NULL, L"保存Workbook失败!", L"错误", MB_OK);
pWorksheet->Release();
pWorksheets->Release();
pWorkbook->Release();
pWorkbooks->Release();
pExcelApp->Release();
return;
}
// 关闭Workbook
hr = pWorkbook->Invoke(0x00000117, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, NULL, NULL, NULL, NULL);
if (FAILED(hr))
{
MessageBox(NULL, L"关闭Workbook失败!", L"错误", MB_OK);
}
// 释放对象
pWorksheet->Release();
pWorksheets->Release();
pWorkbook->Release();
pWorkbooks->Release();
pExcelApp->Release();
// 终止COM库
CoUninitialize();
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WriteExcel();
return 0;
}
```
这个例子通过OLE Automation技术调用Excel的COM接口,实现了向Excel中写入一行数据的功能。编写WIN32 API程序需要有基本的C语言和Windows API编程知识。
阅读全文