代码如下:
#include <QCoreApplication>
#include "xlsxdocument.h"
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 创建一个新的 Excel 工作簿
QXlsx::Document xlsx;
// 定义表头
QStringList headers = {"Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8"};
// 创建一个格式对象,设置背景颜色为灰色
QXlsx::Format headerFormat;
headerFormat.setPatternBackgroundColor(QColor(Qt::lightGray));
// 写入表头,并应用格式
for (int col = 1; col <= headers.size(); ++col) {
xlsx.write(1, col, headers[col - 1], headerFormat);
}
// 写入 6 行记录
for (int row = 2; row <= 7; ++row) {
for (int col = 1; col <= 8; ++col) {
xlsx.write(row, col, QString("Data %1-%2").arg(row - 1).arg(col));
}
}
// 保存工作簿
if (xlsx.saveAs("example.xlsx")) {
qDebug() << "Excel file saved successfully.";
} else {
qDebug() << "Failed to save Excel file.";
}
return a.exec();
}
2965

被折叠的 条评论
为什么被折叠?



