win32 api读写excel表格 源码下载
时间: 2023-08-19 16:04:51 浏览: 144
以下是使用Win32 API读写Excel表格的示例代码:
```c++
#include <windows.h>
#include <ole2.h>
#include <ocidl.h>
#include <olectl.h>
int main()
{
CoInitialize(NULL);
// 创建Excel应用程序
CLSID clsid;
LPUNKNOWN pUnk;
IDispatch* pApp;
HRESULT hr;
hr = CLSIDFromProgID(L"Excel.Application", &clsid);
if (FAILED(hr))
{
CoUninitialize();
return -1;
}
hr = GetActiveObject(clsid, NULL, &pUnk);
if (FAILED(hr))
{
hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void**)&pApp);
if (FAILED(hr))
{
CoUninitialize();
return -1;
}
}
else
{
hr = pUnk->QueryInterface(IID_IDispatch, (void**)&pApp);
pUnk->Release();
if (FAILED(hr))
{
CoUninitialize();
return -1;
}
}
// 获取Workbooks对象
VARIANT result;
DISPPARAMS params = { NULL, NULL, 0, 0 };
hr = pApp->Invoke(DISPID_WORKBOOKS, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, ¶ms, &result, NULL, NULL);
if (FAILED(hr))
{
pApp->Release();
CoUninitialize();
return -1;
}
IDispatch* pWorkbooks = result.pdispVal;
// 打开Excel文件
VARIANT vFilename;
vFilename.vt = VT_BSTR;
vFilename.bstrVal = SysAllocString(L"D:\\test.xlsx");
hr = pWorkbooks->Invoke(DISPID_WORKBOOKS_OPEN, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, ¶ms, &result, NULL, NULL);
if (FAILED(hr))
{
VariantClear(&vFilename);
pWorkbooks->Release();
pApp->Release();
CoUninitialize();
return -1;
}
IDispatch* pWorkbook = result.pdispVal;
// 获取Worksheets对象
hr = pWorkbook->Invoke(DISPID_WORKSHEETS, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, ¶ms, &result, NULL, NULL);
if (FAILED(hr))
{
pWorkbook->Release();
pWorkbooks->Release();
pApp->Release();
CoUninitialize();
return -1;
}
IDispatch* pWorksheets = result.pdispVal;
// 获取Worksheet对象
VARIANT vIndex;
vIndex.vt = VT_I4;
vIndex.lVal = 1;
hr = pWorksheets->Invoke(DISPID_ITEM, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, ¶ms, &result, NULL, NULL);
if (FAILED(hr))
{
pWorksheets->Release();
pWorkbook->Release();
pWorkbooks->Release();
pApp->Release();
CoUninitialize();
return -1;
}
IDispatch* pWorksheet = result.pdispVal;
// 读取单元格的值
VARIANT vCell;
vCell.vt = VT_BSTR;
vCell.bstrVal = SysAllocString(L"A1");
hr = pWorksheet->Invoke(DISPID_RANGE, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, ¶ms, &result, NULL, NULL);
if (FAILED(hr))
{
VariantClear(&vCell);
pWorksheet->Release();
pWorksheets->Release();
pWorkbook->Release();
pWorkbooks->Release();
pApp->Release();
CoUninitialize();
return -1;
}
IDispatch* pRange = result.pdispVal;
hr = pRange->Invoke(DISPID_VALUE, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, ¶ms, &result, NULL, NULL);
if (FAILED(hr))
{
pRange->Release();
VariantClear(&vCell);
pWorksheet->Release();
pWorksheets->Release();
pWorkbook->Release();
pWorkbooks->Release();
pApp->Release();
CoUninitialize();
return -1;
}
VARIANT vValue = result;
if (vValue.vt == VT_BSTR)
{
wprintf(L"%s\n", vValue.bstrVal);
}
VariantClear(&vValue);
pRange->Release();
// 写入单元格的值
VARIANT vValue2;
vValue2.vt = VT_BSTR;
vValue2.bstrVal = SysAllocString(L"Hello, world!");
hr = pWorksheet->Invoke(DISPID_RANGE, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, ¶ms, &result, NULL, NULL);
if (FAILED(hr))
{
VariantClear(&vValue2);
VariantClear(&vCell);
pWorksheet->Release();
pWorksheets->Release();
pWorkbook->Release();
pWorkbooks->Release();
pApp->Release();
CoUninitialize();
return -1;
}
pRange = result.pdispVal;
hr = pRange->Invoke(DISPID_VALUE, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPUT, ¶ms, &vValue2, NULL, NULL);
if (FAILED(hr))
{
pRange->Release();
VariantClear(&vValue2);
VariantClear(&vCell);
pWorksheet->Release();
pWorksheets->Release();
pWorkbook->Release();
pWorkbooks->Release();
pApp->Release();
CoUninitialize();
return -1;
}
pRange->Release();
VariantClear(&vValue2);
// 关闭Excel文件
VARIANT vSaveChanges;
vSaveChanges.vt = VT_BOOL;
vSaveChanges.boolVal = VARIANT_FALSE;
hr = pWorkbook->Invoke(DISPID_WORKBOOK_CLOSE, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, ¶ms, &result, NULL, NULL);
if (FAILED(hr))
{
VariantClear(&vCell);
pWorksheet->Release();
pWorksheets->Release();
pWorkbook->Release();
pWorkbooks->Release();
pApp->Release();
CoUninitialize();
return -1;
}
pWorkbook->Release();
VariantClear(&vCell);
pWorksheets->Release();
pWorkbooks->Release();
// 退出Excel应用程序
hr = pApp->Invoke(DISPID_APPLICATION_QUIT, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, ¶ms, NULL, NULL, NULL);
pApp->Release();
CoUninitialize();
return 0;
}
```
请注意,使用Win32 API读写Excel表格需要使用COM技术,因此需要在程序中包含OLE2.H头文件,并且需要在程序开始时调用CoInitialize函数进行初始化。
此外,该示例代码使用了Excel对象模型中的各种方法和属性,因此需要对Excel对象模型有一定的了解才能理解代码。如果您不熟悉Excel对象模型,请先学习相关知识再尝试使用该示例代码。
阅读全文