c++ excel

C++中怎么把二维数组将字符串写入excel

小亿
115
2023-08-04 11:50:31
栏目: 编程语言

要将二维数组写入Excel,可以使用第三方库,比如libxl或者xlsxwriter。以下是使用libxl的示例代码:

#include <iostream>
#include <iomanip>
#include "libxl.h"
using namespace libxl;
int main() {
int rowCount = 3;
int colCount = 4;
const char* fileName = "output.xlsx";
Book* book = xlCreateBook();
Sheet* sheet = book->addSheet("Sheet1");
const char* data[rowCount][colCount] = {
{ "Name", "Age", "Gender", "City" },
{ "John", "25", "Male", "New York" },
{ "Amy", "30", "Female", "Los Angeles" }
};
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < colCount; j++) {
sheet->writeStr(i+1, j+1, data[i][j], 0);
}
}
if (book->save(fileName)) {
std::cout << "Excel file saved successfully!" << std::endl;
} else {
std::cout << "Error saving Excel file!" << std::endl;
}
book->release();
return 0;
}

这个示例代码创建了一个3x4的二维数组,并将其写入名为"output.xlsx"的Excel文件中的Sheet1工作表。要运行此代码,需要先下载并安装libxl库,并在代码中包含libxl.h头文件。

另外,如果要使用xlsxwriter库,可以参考它的官方文档和示例代码,使用类似的方法将二维数组写入Excel。

0
看了该问题的人还看了