C语言 DISPID_WORKBOOKS_OPEN 打开excel表格 读取表格Rows 源码
时间: 2023-07-31 09:11:03 浏览: 166
以下是使用 C语言和 Microsoft Excel COM对象模型打开Excel表格并读取表格Rows的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ole2.h>
int main()
{
HRESULT hr;
CoInitialize(NULL);
// 创建Excel Application对象
IDispatch *pExcelApp;
hr = CoCreateInstance(&CLSID_Application, NULL, CLSCTX_LOCAL_SERVER, &IID_IDispatch, (void **)&pExcelApp);
if (FAILED(hr))
{
printf("Failed to create Excel Application object. Error code: %d\n", hr);
CoUninitialize();
return 1;
}
// 设置Visible属性为True
VARIANT vtVisible;
vtVisible.vt = VT_BOOL;
vtVisible.boolVal = TRUE;
DISPID dispid;
LPOLESTR lpstr = L"Visible";
hr = pExcelApp->GetIDsOfNames(IID_NULL, &lpstr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
hr = pExcelApp->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYPUT, &vtVisible, NULL, NULL, NULL);
// 打开工作簿
IDispatch *pWorkbooks;
lpstr = L"Workbooks";
hr = pExcelApp->GetIDsOfNames(IID_NULL, &lpstr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
VARIANT result;
hr = pExcelApp->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, NULL, &result, NULL, NULL);
pWorkbooks = result.pdispVal;
IDispatch *pWorkbook;
lpstr = L"Open";
VARIANT vtFilename, vtUpdateLinks, vtReadOnly, vtFormat, vtPassword, vtWriteResPassword, vtIgnoreReadOnlyRecommended, vtOrigin, vtDelimiter, vtEditable, vtNotify;
vtFilename.vt = VT_BSTR;
vtFilename.bstrVal = SysAllocString(L"C:\\test.xlsx");
vtUpdateLinks.vt = VT_BOOL;
vtUpdateLinks.boolVal = FALSE;
vtReadOnly.vt = VT_BOOL;
vtReadOnly.boolVal = FALSE;
vtFormat.vt = VT_BOOL;
vtFormat.boolVal = FALSE;
vtPassword.vt = VT_BSTR;
vtPassword.bstrVal = SysAllocString(L"");
vtWriteResPassword.vt = VT_BSTR;
vtWriteResPassword.bstrVal = SysAllocString(L"");
vtIgnoreReadOnlyRecommended.vt = VT_BOOL;
vtIgnoreReadOnlyRecommended.boolVal = FALSE;
vtOrigin.vt = VT_I4;
vtOrigin.lVal = 0;
vtDelimiter.vt = VT_BSTR;
vtDelimiter.bstrVal = SysAllocString(L",");
vtEditable.vt = VT_BOOL;
vtEditable.boolVal = FALSE;
vtNotify.vt = VT_BOOL;
vtNotify.boolVal = FALSE;
hr = pWorkbooks->GetIDsOfNames(IID_NULL, &lpstr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
hr = pWorkbooks->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &vtFilename, &result, NULL, NULL);
pWorkbook = result.pdispVal;
// 打开表格
IDispatch *pWorksheets;
lpstr = L"Worksheets";
hr = pWorkbook->GetIDsOfNames(IID_NULL, &lpstr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
hr = pWorkbook->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, NULL, &result, NULL, NULL);
pWorksheets = result.pdispVal;
IDispatch *pWorksheet;
lpstr = L"Item";
VARIANT vtIndex;
vtIndex.vt = VT_I4;
vtIndex.lVal = 1;
hr = pWorksheets->GetIDsOfNames(IID_NULL, &lpstr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
hr = pWorksheets->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &vtIndex, &result, NULL, NULL);
pWorksheet = result.pdispVal;
// 获取表格数据
lpstr = L"Rows";
IDispatch *pRows;
hr = pWorksheet->GetIDsOfNames(IID_NULL, &lpstr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
hr = pWorksheet->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, NULL, &result, NULL, NULL);
pRows = result.pdispVal;
lpstr = L"Count";
hr = pRows->GetIDsOfNames(IID_NULL, &lpstr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
hr = pRows->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, NULL, &result, NULL, NULL);
int rowCount = result.lVal;
// 输出表格数据
for (int i = 1; i <= rowCount; i++)
{
lpstr = L"Item";
VARIANT vtRow;
vtRow.vt = VT_I4;
vtRow.lVal = i;
hr = pRows->GetIDsOfNames(IID_NULL, &lpstr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
hr = pRows->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &vtRow, &result, NULL, NULL);
IDispatch *pRow = result.pdispVal;
lpstr = L"Cells";
IDispatch *pCells;
hr = pRow->GetIDsOfNames(IID_NULL, &lpstr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
hr = pRow->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, NULL, &result, NULL, NULL);
pCells = result.pdispVal;
lpstr = L"Count";
hr = pCells->GetIDsOfNames(IID_NULL, &lpstr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
hr = pCells->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, NULL, &result, NULL, NULL);
int cellCount = result.lVal;
for (int j = 1; j <= cellCount; j++)
{
lpstr = L"Item";
VARIANT vtCell;
vtCell.vt = VT_I4;
vtCell.lVal = j;
hr = pCells->GetIDsOfNames(IID_NULL, &lpstr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
hr = pCells->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &vtCell, &result, NULL, NULL);
IDispatch *pCell = result.pdispVal;
VARIANT vtValue;
lpstr = L"Value";
hr = pCell->GetIDsOfNames(IID_NULL, &lpstr, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
hr = pCell->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, NULL, &vtValue, NULL, NULL);
if (vtValue.vt == VT_BSTR)
{
wprintf(L"%ls\t", vtValue.bstrVal);
}
else
{
printf("%d\t", vtValue.lVal);
}
VariantClear(&vtValue);
pCell->Release();
}
printf("\n");
pCells->Release();
pRow->Release();
}
// 释放对象
VariantClear(&vtFilename);
VariantClear(&vtPassword);
VariantClear(&vtWriteResPassword);
VariantClear(&vtDelimiter);
pRows->Release();
pWorksheet->Release();
pWorksheets->Release();
pWorkbook->Release();
pWorkbooks->Release();
pExcelApp->Release();
CoUninitialize();
return 0;
}
```
请注意,此代码使用了Microsoft Excel COM对象模型,因此需要安装Microsoft Excel才能使用。此外,需要将代码中的文件路径`C:\\test.xlsx`替换为您要读取的实际表格路径。
阅读全文