C语言 Win32 api Worksheets 读写excel表格实例
时间: 2024-01-22 09:21:16 浏览: 123
以下是一个利用 C 语言 Win32 API 实现读写 Excel 表格的示例:
```
#include <windows.h>
#include <ole2.h>
#include <stdio.h>
void ReadExcel()
{
LPDISPATCH lpDisp;
VARIANT var;
CLSID clsid;
HRESULT hr;
LPTSTR pszFile = TEXT("test.xls");
// 初始化 COM 库
CoInitialize(NULL);
// 获取 Excel 应用程序对象
hr = CLSIDFromProgID(L"Excel.Application", &clsid);
if (FAILED(hr))
{
printf("Failed to get Excel Application CLSID!\n");
return;
}
hr = CoCreateInstance(&clsid, NULL, CLSCTX_LOCAL_SERVER, &IID_IDispatch, (void**)&lpDisp);
if (FAILED(hr))
{
printf("Failed to create Excel Application object!\n");
return;
}
// 打开 Excel 文件
VARIANT vtFalse = {0};
vtFalse.vt = VT_BOOL;
vtFalse.boolVal = VARIANT_FALSE;
VARIANT vtFileName;
vtFileName.vt = VT_BSTR;
vtFileName.bstrVal = SysAllocString(pszFile);
hr = IDispatch_Invoke(lpDisp, 0x783, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &vtFalse, NULL, NULL, NULL);
if (FAILED(hr))
{
printf("Failed to open Excel file %s!\n", pszFile);
return;
}
// 获取工作表对象
IDispatch* pWorkbook = NULL;
hr = IDispatch_Invoke(lpDisp, 0x50, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, NULL, NULL, NULL, &var);
if (FAILED(hr))
{
printf("Failed to get Workbooks object!\n");
return;
}
pWorkbook = var.pdispVal;
if (!pWorkbook)
{
printf("Failed to get Workbook object!\n");
return;
}
IDispatch* pWorksheet = NULL;
VARIANT vtIndex;
vtIndex.vt = VT_I4;
vtIndex.lVal = 1;
hr = IDispatch_Invoke(pWorkbook, 0x22e, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &vtIndex, NULL, NULL, &var);
if (FAILED(hr))
{
printf("Failed to get Worksheet object!\n");
return;
}
pWorksheet = var.pdispVal;
if (!pWorksheet)
{
printf("Failed to get Worksheet object!\n");
return;
}
// 读取单元格数据
VARIANT vtRange;
vtRange.vt = VT_BSTR;
vtRange.bstrVal = SysAllocString(L"A1");
hr = IDispatch_Invoke(pWorksheet, 0x6c, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &vtRange, NULL, &var, NULL);
if (FAILED(hr))
{
printf("Failed to get cell value!\n");
return;
}
printf("A1: %ls\n", var.bstrVal);
// 写入单元格数据
VARIANT vtValue;
vtValue.vt = VT_BSTR;
vtValue.bstrVal = SysAllocString(L"Hello, world!");
hr = IDispatch_Invoke(pWorksheet, 0x6c, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYPUT, &vtRange, &vtValue, NULL, NULL);
if (FAILED(hr))
{
printf("Failed to set cell value!\n");
return;
}
// 保存并关闭 Excel 文件
hr = IDispatch_Invoke(pWorkbook, 0x1f4, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &vtFalse, NULL, NULL, NULL);
if (FAILED(hr))
{
printf("Failed to save Excel file %s!\n", pszFile);
return;
}
hr = IDispatch_Invoke(pWorkbook, 0x1f3, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, NULL, NULL, NULL, NULL);
if (FAILED(hr))
{
printf("Failed to close Excel file %s!\n", pszFile);
return;
}
// 释放资源
SysFreeString(vtFileName.bstrVal);
SysFreeString(vtRange.bstrVal);
SysFreeString(vtValue.bstrVal);
pWorksheet->Release();
pWorkbook->Release();
lpDisp->Release();
// 反初始化 COM 库
CoUninitialize();
}
int main()
{
ReadExcel();
return 0;
}
```
该示例主要利用了 Excel 应用程序对象和工作表对象的 IDispatch 接口来实现读写 Excel 表格。其中,通过调用 `CLSIDFromProgID` 和 `CoCreateInstance` 获取 Excel 应用程序对象,然后通过调用 `IDispatch_Invoke` 方法打开 Excel 文件和获取工作表对象;接着,通过调用 `IDispatch_Invoke` 方法获取单元格数据和写入单元格数据;最后,通过调用 `IDispatch_Invoke` 方法保存并关闭 Excel 文件,释放资源并反初始化 COM 库。
阅读全文