C语言 DISPID_WORKBOOKS_OPEN 打开excel表格 读取表格内容 读取Rows
时间: 2023-07-31 22:11:02 浏览: 200
使用C语言打开Excel表格并读取表格内容需要使用Microsoft Excel Object Library,以下是一个简单的例子:
```c
#include <windows.h>
#include <ole2.h>
#include "excel9.h" // Excel 2000
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")
#pragma comment(lib, "excel9.lib")
int main()
{
// 初始化COM
CoInitialize(NULL);
// 创建Excel应用程序对象
IDispatch* pExcel;
CoCreateInstance(CLSID_Application, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void**)&pExcel);
// 获取Workbooks集合对象
IDispatch* pWorkbooks;
{
VARIANT result;
VariantInit(&result);
DISPPARAMS params = { NULL, NULL, 0, 0 };
pExcel->Invoke(DISPID_WORKBOOKS, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, ¶ms, &result, NULL, NULL);
pWorkbooks = result.pdispVal;
}
// 打开Excel表格
IDispatch* pWorkbook;
{
VARIANT result;
VariantInit(&result);
DISPPARAMS params = { NULL, NULL, 0, 0 };
VARIANT filename;
filename.vt = VT_BSTR;
filename.bstrVal = SysAllocString(L"path/to/your/excel/file.xlsx");
params.cArgs = 1;
params.rgvarg = &filename;
pWorkbooks->Invoke(DISPID_WORKBOOKS_OPEN, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, ¶ms, &result, NULL, NULL);
pWorkbook = result.pdispVal;
}
// 获取Worksheets集合对象
IDispatch* pWorksheets;
{
VARIANT result;
VariantInit(&result);
DISPPARAMS params = { NULL, NULL, 0, 0 };
pWorkbook->Invoke(DISPID_WORKBOOK_WORKSHEETS, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, ¶ms, &result, NULL, NULL);
pWorksheets = result.pdispVal;
}
// 获取第一个Worksheet对象
IDispatch* pWorksheet;
{
VARIANT result;
VariantInit(&result);
DISPPARAMS params = { NULL, NULL, 0, 0 };
VARIANT index;
index.vt = VT_I4;
index.lVal = 1;
params.cArgs = 1;
params.rgvarg = &index;
pWorksheets->Invoke(DISPID_VALUE, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, ¶ms, &result, NULL, NULL);
pWorksheet = result.pdispVal;
}
// 读取行数和列数
int rowCount, colCount;
{
VARIANT result;
VariantInit(&result);
DISPPARAMS params = { NULL, NULL, 0, 0 };
pWorksheet->Invoke(DISPID_VALUE, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, ¶ms, &result, NULL, NULL);
IDispatch* pRange = result.pdispVal;
VARIANT rowCountVar, colCountVar;
VariantInit(&rowCountVar);
VariantInit(&colCountVar);
params.cArgs = 2;
params.rgvarg = (VARIANTARG*)&rowCountVar;
pRange->Invoke(DISPID_VALUE, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, ¶ms, &result, NULL, NULL);
rowCount = rowCountVar.lVal;
params.rgvarg = (VARIANTARG*)&colCountVar;
pRange->Invoke(DISPID_VALUE, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, ¶ms, &result, NULL, NULL);
colCount = colCountVar.lVal;
pRange->Release();
}
// 读取数据
for (int i = 1; i <= rowCount; i++)
{
for (int j = 1; j <= colCount; j++)
{
VARIANT result;
VariantInit(&result);
DISPPARAMS params = { NULL, NULL, 0, 0 };
VARIANT row, col;
row.vt = VT_I4;
col.vt = VT_I4;
row.lVal = i;
col.lVal = j;
params.cArgs = 2;
params.rgvarg = (VARIANTARG*)&col;
pWorksheet->Invoke(DISPID_VALUE, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, ¶ms, &result, NULL, NULL);
IDispatch* pCell = result.pdispVal;
BSTR value;
pCell->Invoke(DISPID_VALUE, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, ¶ms, &result, NULL, NULL);
value = result.bstrVal;
printf("%S\t", value);
SysFreeString(value);
pCell->Release();
}
printf("\n");
}
// 释放资源
pWorksheet->Release();
pWorksheets->Release();
pWorkbook->Release();
pWorkbooks->Release();
pExcel->Release();
CoUninitialize();
return 0;
}
```
这个例子使用了DISPID_WORKBOOKS_OPEN来打开Excel表格,然后通过DISPID_WORKBOOK_WORKSHEETS和DISPID_VALUE来获取第一个Worksheet对象,以及获取行数和列数。接着,它通过DISPID_VALUE和DISPATCH_PROPERTYGET来获取单元格的值。需要注意的是,对于每一个DISPATCH对象,我们都需要在使用完之后调用Release()方法来释放资源。
阅读全文