将二维数组写进excel中 C++,不使用第三方库
时间: 2024-03-01 17:53:41 浏览: 128
在C++中,可以使用Windows API来操作Excel,以下是一个示例代码,可以将二维数组写入Excel中:
```c++
#include <Windows.h>
#include <string>
void writeToExcel(int** data, int rows, int cols, std::string fileName)
{
// 初始化COM库
CoInitialize(NULL);
// 创建Excel应用程序对象
IDispatch* pExcelApp;
CLSID clsid;
CLSIDFromProgID(L"Excel.Application", &clsid);
CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void**)&pExcelApp);
// 设置Visible属性为true,即可看到Excel打开的界面
VARIANT var;
var.vt = VT_BOOL;
var.boolVal = TRUE;
IDispatch* pExcelAppDisp;
pExcelApp->QueryInterface(IID_IDispatch, (void**)&pExcelAppDisp);
DISPID dispid;
LPOLESTR szMember = L"Visible";
pExcelAppDisp->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
pExcelAppDisp->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYPUT, &var, NULL, NULL, NULL);
// 获取Workbooks集合对象
IDispatch* pWorkbooks;
szMember = L"Workbooks";
pExcelAppDisp->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
VARIANT result;
pExcelAppDisp->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &result, NULL, NULL, NULL);
pWorkbooks = result.pdispVal;
// 打开Excel文件或创建一个新文件
IDispatch* pWorkbook;
VARIANT vFileName;
vFileName.vt = VT_BSTR;
vFileName.bstrVal = SysAllocStringLen(NULL, fileName.size());
MultiByteToWideChar(CP_ACP, 0, fileName.c_str(), fileName.size(), vFileName.bstrVal, fileName.size());
szMember = L"Open";
pWorkbooks->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
pWorkbooks->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &result, NULL, NULL, NULL);
pWorkbook = result.pdispVal;
// 获取Worksheets集合对象
IDispatch* pWorksheets;
szMember = L"Worksheets";
pWorkbook->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
pWorkbook->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &result, NULL, NULL, NULL);
pWorksheets = result.pdispVal;
// 获取第一个Worksheet对象
IDispatch* pWorksheet;
szMember = L"Item";
VARIANT index;
index.vt = VT_I4;
index.lVal = 1;
pWorksheets->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
pWorksheets->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &result, 1, &index, NULL);
pWorksheet = result.pdispVal;
// 将数据写入Worksheet中
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
VARIANT value;
value.vt = VT_I4;
value.lVal = data[i][j];
szMember = L"Cells";
pWorksheet->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
VARIANT xIndex;
xIndex.vt = VT_I4;
xIndex.lVal = i + 1;
VARIANT yIndex;
yIndex.vt = VT_I4;
yIndex.lVal = j + 1;
VARIANT cell;
VARIANT params[2] = { xIndex, yIndex };
pWorksheet->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &cell, 2, params, NULL);
szMember = L"Value";
cell.pdispVal->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
cell.pdispVal->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYPUT, &value, NULL, NULL, NULL);
}
}
// 保存并关闭Excel文件
szMember = L"Save";
pWorkbook->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
pWorkbook->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, NULL, NULL, NULL, NULL);
szMember = L"Close";
pWorkbook->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
pWorkbook->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, NULL, NULL, NULL, NULL);
// 释放COM资源
pExcelAppDisp->Release();
pExcelApp->Release();
CoUninitialize();
}
```
调用示例:
```c++
int data[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
writeToExcel((int**)data, 3, 4, "test.xlsx");
```
该示例代码中,使用了Windows API的COM接口来操作Excel,实现了将二维数组写入Excel的功能。
阅读全文