C语言 Win32API 读取Ecxel表格实例 Excel.Application源码
时间: 2023-11-28 21:47:37 浏览: 93
以下是使用 C 语言 Win32API 实现读取 Excel 表格的示例代码:
```
#include <windows.h>
#include <ole2.h> // 需要引入 OLE2 库
#include <olectl.h>
#include <ocidl.h>
int main()
{
// 初始化 COM 库
CoInitialize(NULL);
// 创建 Excel 应用程序实例
CLSID clsid;
CLSIDFromProgID(L"Excel.Application", &clsid);
IDispatch* pExcel = NULL;
CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void **)&pExcel);
// 打开 Excel 文件
VARIANT vtFilename;
vtFilename.vt = VT_BSTR;
vtFilename.bstrVal = SysAllocString(L"C:\\test.xlsx");
VARIANT vtReadOnly;
vtReadOnly.vt = VT_BOOL;
vtReadOnly.boolVal = TRUE;
VARIANT vtMissing;
vtMissing.vt = VT_ERROR;
vtMissing.scode = DISP_E_PARAMNOTFOUND;
VARIANT vtWorkbooks;
DISPID dispid;
DISPPARAMS dispparams = {&vtFilename, &vtMissing, &vtReadOnly, NULL};
pExcel->GetIDsOfNames(IID_NULL, L"Workbooks", 1, LOCALE_SYSTEM_DEFAULT, &dispid);
pExcel->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &dispparams, &vtWorkbooks, NULL, NULL);
// 获取活动工作簿
VARIANT vtActiveWorkbook;
dispid = 0x0000003D; // ActiveWorkbook 的 DISPID
dispparams.cArgs = 0;
dispparams.cNamedArgs = 0;
pExcel->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &dispparams, &vtActiveWorkbook, NULL, NULL);
// 获取活动工作表
VARIANT vtActiveSheet;
dispid = 0x0000002B; // ActiveSheet 的 DISPID
dispparams.cArgs = 0;
dispparams.cNamedArgs = 0;
vtActiveSheet.vt = VT_DISPATCH;
vtActiveSheet.pdispVal = NULL;
pExcel->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &dispparams, &vtActiveSheet, NULL, NULL);
// 获取活动工作表的名称
VARIANT vtName;
dispid = 0x00000006; // Name 的 DISPID
dispparams.cArgs = 0;
dispparams.cNamedArgs = 0;
pActiveSheet->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &dispparams, &vtName, NULL, NULL);
wprintf(L"Active worksheet name: %s\n", vtName.bstrVal);
// 获取行数和列数
VARIANT vtRows;
VARIANT vtColumns;
dispparams.cArgs = 1;
dispparams.cNamedArgs = 1;
DISPID dispidRows;
DISPID dispidColumns;
BSTR bstrRows = SysAllocString(L"Rows");
BSTR bstrColumns = SysAllocString(L"Columns");
pActiveSheet->GetIDsOfNames(IID_NULL, &bstrRows, 1, LOCALE_SYSTEM_DEFAULT, &dispidRows);
pActiveSheet->GetIDsOfNames(IID_NULL, &bstrColumns, 1, LOCALE_SYSTEM_DEFAULT, &dispidColumns);
dispparams.cArgs = 0;
dispparams.cNamedArgs = 0;
pActiveSheet->Invoke(dispidRows, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &dispparams, &vtRows, NULL, NULL);
pActiveSheet->Invoke(dispidColumns, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &dispparams, &vtColumns, NULL, NULL);
wprintf(L"Number of rows: %d\n", vtRows.lVal);
wprintf(L"Number of columns: %d\n", vtColumns.lVal);
// 读取单元格内容
VARIANT vtCellValue;
dispid = 0x00000000; // Item 的 DISPID
dispparams.cArgs = 2;
dispparams.cNamedArgs = 0;
VARIANT vtRow;
vtRow.vt = VT_I4;
vtRow.lVal = 1;
VARIANT vtColumn;
vtColumn.vt = VT_I4;
vtColumn.lVal = 1;
dispparams.rgvarg = &vtColumn;
dispparams.rgvarg[1] = &vtRow;
pActiveSheet->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &dispparams, &vtCellValue, NULL, NULL);
wprintf(L"Cell value at (1, 1): %s\n", vtCellValue.bstrVal);
// 释放资源
VariantClear(&vtFilename);
VariantClear(&vtReadOnly);
VariantClear(&vtMissing);
VariantClear(&vtWorkbooks);
VariantClear(&vtActiveWorkbook);
VariantClear(&vtActiveSheet);
VariantClear(&vtName);
VariantClear(&vtRows);
VariantClear(&vtColumns);
VariantClear(&vtCellValue);
SysFreeString(vtName.bstrVal);
SysFreeString(bstrRows);
SysFreeString(bstrColumns);
pActiveSheet->Release();
pActiveWorkbook->Release();
pWorkbooks->Release();
pExcel->Release();
// 反初始化 COM 库
CoUninitialize();
return 0;
}
```
注意:运行此代码需要在编译选项中添加 OLE2 库的链接。
阅读全文