C语言 Win32API 读取Ecxel表格实例
时间: 2023-11-28 09:47:13 浏览: 155
以下是使用 C语言和 Win32API 读取Excel表格的示例代码:
```c
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <ole2.h>
#include <olectl.h>
#include <stddef.h>
#include <stdlib.h>
// 声明Excel COM对象
IDispatch *pApp = NULL;
IDispatch *pBooks = NULL;
IDispatch *pBook = NULL;
IDispatch *pSheets = NULL;
IDispatch *pSheet = NULL;
IDispatch *pRange = NULL;
// Excel的文件路径
TCHAR szExcelPath[MAX_PATH] = _T("E:\\test.xlsx");
// 初始化Excel应用程序
BOOL InitExcel()
{
CoInitialize(NULL);
CLSID clsid;
HRESULT hr = CLSIDFromProgID(L"Excel.Application", &clsid);
if (FAILED(hr))
{
return FALSE;
}
hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void **)&pApp);
if (FAILED(hr))
{
return FALSE;
}
return TRUE;
}
// 打开Excel文件
BOOL OpenExcelFile()
{
// 获取Workbooks对象
VARIANT vtEmpty = { 0 };
DISPID dispid;
hr = pApp->GetIDsOfNames(L"Workbooks", &iid_NULL, 1, LOCALE_USER_DEFAULT, &dispid);
if (FAILED(hr))
{
return FALSE;
}
hr = pApp->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dpNoArgs, &vtBooks, NULL, NULL);
if (FAILED(hr))
{
return FALSE;
}
// 打开Excel文件
VARIANT vFileName;
vFileName.vt = VT_BSTR;
vFileName.bstrVal = SysAllocString(szExcelPath);
hr = vtBooks.pdispVal->GetIDsOfNames(L"Open", &iid_NULL, 1, LOCALE_USER_DEFAULT, &dispid);
if (FAILED(hr))
{
return FALSE;
}
hr = vtBooks.pdispVal->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dpFile, &vFileName, NULL, NULL);
if (FAILED(hr))
{
return FALSE;
}
// 获取打开的Workbook对象
VARIANT vtIndex;
vtIndex.vt = VT_I4;
vtIndex.lVal = 1;
hr = vtBooks.pdispVal->GetIDsOfNames(L"Item", &iid_NULL, 1, LOCALE_USER_DEFAULT, &dispid);
if (FAILED(hr))
{
return FALSE;
}
hr = vtBooks.pdispVal->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dpIndex, &vtIndex, &vtBook, NULL);
if (FAILED(hr))
{
return FALSE;
}
// 获取Worksheets对象
hr = vtBook.pdispVal->GetIDsOfNames(L"Sheets", &iid_NULL, 1, LOCALE_USER_DEFAULT, &dispid);
if (FAILED(hr))
{
return FALSE;
}
hr = vtBook.pdispVal->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dpNoArgs, &vtSheets, NULL, NULL);
if (FAILED(hr))
{
return FALSE;
}
// 获取Worksheet对象
vtIndex.lVal = 1;
hr = vtSheets.pdispVal->GetIDsOfNames(L"Item", &iid_NULL, 1, LOCALE_USER_DEFAULT, &dispid);
if (FAILED(hr))
{
return FALSE;
}
hr = vtSheets.pdispVal->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dpIndex, &vtIndex, &vtSheet, NULL);
if (FAILED(hr))
{
return FALSE;
}
// 获取Range对象
VARIANT vtRange;
vtRange.vt = VT_BSTR;
vtRange.bstrVal = SysAllocString(L"A1:D4");
hr = vtSheet.pdispVal->GetIDsOfNames(L"Range", &iid_NULL, 1, LOCALE_USER_DEFAULT, &dispid);
if (FAILED(hr))
{
return FALSE;
}
hr = vtSheet.pdispVal->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dpRange, &vtRange, NULL, NULL);
if (FAILED(hr))
{
return FALSE;
}
return TRUE;
}
// 读取Excel数据
BOOL ReadExcelData()
{
VARIANT vtValue;
VariantInit(&vtValue);
DISPPARAMS dpNoArgs = { NULL, NULL, 0, 0 };
DISPID dispid;
HRESULT hr = pRange->GetIDsOfNames(L"Value", &iid_NULL, 1, LOCALE_USER_DEFAULT, &dispid);
if (FAILED(hr))
{
return FALSE;
}
hr = pRange->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dpNoArgs, &vtValue, NULL, NULL);
if (FAILED(hr))
{
return FALSE;
}
// 打印Excel表格数据
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
BSTR bstrData = vtValue.parray->pvData + vtValue.parray->cbElements * (i * 4 + j);
printf("%S\t", bstrData);
}
printf("\n");
}
return TRUE;
}
// 程序入口函数
int _tmain(int argc, _TCHAR* argv[])
{
if (!InitExcel())
{
printf("初始化Excel应用程序失败");
return -1;
}
if (!OpenExcelFile())
{
printf("打开Excel文件失败");
return -1;
}
if (!ReadExcelData())
{
printf("读取Excel数据失败");
return -1;
}
return 0;
}
```
在使用前需要将 `szExcelPath` 变量修改为实际的Excel文件路径。此代码中使用了COM组件的方式读取Excel表格数据,需要注意的是,在使用完毕后需要调用 `CoUninitialize` 函数释放COM资源。
阅读全文