一、前言
操作excel方式有:QAxObject 和QtXlsx
区别:Qt自带的QAxObject库操作excel的前提是电脑已经安装微软的Office(包含EXCEL),而QtXlsx可以直接使用免装Office且操作更简单。
二、QtXlsx操作示例
参考:https://blog.csdn.net/CUBE_lotus/article/details/127130319
下载源码(qtxlsxwriter-master)
下载地址:dbzhang800/QtXlsxWriter: .xlsx file reader and writer for Qt5 (github.com)
(删除其他只留src,example中有许多使用的样例可以查看学习),并将源码文件放入项目文件下

.Pro工程文件中引入QtXlsx
include(./qtxlsxwriter-master/src/xlsx/qtxlsx.pri)

使用
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "xlsxdocument.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QXlsx::Document xlsx;
xlsx.read("Text.xlsx");
QXlsx::Format formatRed;
formatRed.setPatternBackgroundColor(Qt::red);//设置单元格背景色
xlsx.write(1, 2, "Hello Qt!");
xlsx.write(2, 2, "中文111",formatRed);
xlsx.saveAs("Text1.xlsx");
}
MainWindow::~MainWindow()
{
delete ui;
}
结果:构建目录下新生成Test1.xlsx

更多功能
可参考example示例
或参考:
https://it.cha138.com/shida/show-480896.html
三、QAxObject操作excel
参考https://zhangzc.blog.csdn.net/article/details/106213607