Qt 使用QtXlsx操作Excel表
1.环境搭建
QtXlsx是一个用于读写Microsoft Excel文件(.xlsx)的Qt库。它提供了一组简单易用的API,可以方便地处理电子表格数据。
Github下载:GitHub - dbzhang800/QtXlsxWriter: .xlsx file reader and writer for Qt5
官方文档:http://qtxlsx.debao.me/
环境搭建
解压压缩包

QtXlsx源码嵌入QTCreator中使用。
新建一个QTCreator窗体项目,将上图src文件夹拷贝到该项目路径中。

将如下代码拷贝到.pro文件中

qmake,编译代码。
2.代码示例
做一个日历表格。

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "xlsxdocument.h"
#include "xlsxchartsheet.h"
#include "xlsxcellrange.h"
#include "xlsxchart.h"
#include "xlsxrichstring.h"
#include "xlsxworkbook.h"
#include <QDate>QTXLSX_USE_NAMESPACEMainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);Document xlsx;QDate today(QDate::currentDate());for (int month = 1; month <= 12; ++month) {xlsx.addSheet(QLocale().monthName(month));xlsx.currentWorksheet()->setGridLinesVisible(false);// the header rowFormat headerStyle;headerStyle.setFontSize(48);headerStyle.setFontColor(Qt::darkBlue);headerStyle.setHorizontalAlignment(Format::AlignHCenter);headerStyle.setVerticalAlignment(Format::AlignVCenter);xlsx.setRowHeight(1, 80);xlsx.write("A1", QString("%1 %2").arg(QLocale().monthName(month)).arg(today.year()));xlsx.mergeCells("A1:N1", headerStyle);// header with month titlesfor (int day = 1; day <= 7; ++day) {Format monthStyle;monthStyle.setFontSize(12);monthStyle.setFontColor(Qt::white);monthStyle.setFontBold(true);monthStyle.setHorizontalAlignment(Format::AlignHCenter);monthStyle.setVerticalAlignment(Format::AlignVCenter);monthStyle.setFillPattern(Format::PatternSolid);monthStyle.setPatternBackgroundColor(Qt::darkBlue);xlsx.setColumnWidth(day * 2 - 1, day * 2 - 1, 5);xlsx.setColumnWidth(day * 2, day * 2, 13);xlsx.write(2, day * 2 - 1, QLocale().dayName(day));xlsx.mergeCells(CellRange(2, day * 2 - 1, 2, day * 2), monthStyle);}QColor borderColor = QColor(Qt::gray);Format weekendLeftStyle;weekendLeftStyle.setFontSize(14);weekendLeftStyle.setFontBold(true);weekendLeftStyle.setHorizontalAlignment(Format::AlignLeft);weekendLeftStyle.setVerticalAlignment(Format::AlignTop);weekendLeftStyle.setPatternBackgroundColor(QColor("#93CCEA"));weekendLeftStyle.setLeftBorderStyle(Format::BorderThin);weekendLeftStyle.setLeftBorderColor(borderColor);weekendLeftStyle.setBottomBorderStyle(Format::BorderThin);weekendLeftStyle.setBottomBorderColor(borderColor);Format weekendRightStyle;weekendRightStyle.setHorizontalAlignment(Format::AlignHCenter);weekendRightStyle.setVerticalAlignment(Format::AlignTop);weekendRightStyle.setPatternBackgroundColor(QColor("#93CCEA"));weekendRightStyle.setRightBorderStyle(Format::BorderThin);weekendRightStyle.setRightBorderColor(borderColor);weekendRightStyle.setBottomBorderStyle(Format::BorderThin);weekendRightStyle.setBottomBorderColor(borderColor);Format workdayLeftStyle;workdayLeftStyle.setHorizontalAlignment(Format::AlignLeft);workdayLeftStyle.setVerticalAlignment(Format::AlignTop);workdayLeftStyle.setPatternBackgroundColor(Qt::white);workdayLeftStyle.setLeftBorderStyle(Format::BorderThin);workdayLeftStyle.setLeftBorderColor(borderColor);workdayLeftStyle.setBottomBorderStyle(Format::BorderThin);workdayLeftStyle.setBottomBorderColor(borderColor);Format workdayRightStyle;workdayRightStyle.setHorizontalAlignment(Format::AlignHCenter);workdayRightStyle.setVerticalAlignment(Format::AlignTop);workdayRightStyle.setPatternBackgroundColor(Qt::white);workdayRightStyle.setRightBorderStyle(Format::BorderThin);workdayRightStyle.setRightBorderColor(borderColor);workdayRightStyle.setBottomBorderStyle(Format::BorderThin);workdayRightStyle.setBottomBorderColor(borderColor);Format greyLeftStyle;greyLeftStyle.setPatternBackgroundColor(Qt::lightGray);greyLeftStyle.setLeftBorderStyle(Format::BorderThin);greyLeftStyle.setLeftBorderColor(borderColor);greyLeftStyle.setBottomBorderStyle(Format::BorderThin);greyLeftStyle.setBottomBorderColor(borderColor);Format greyRightStyle;greyRightStyle.setPatternBackgroundColor(Qt::lightGray);greyRightStyle.setRightBorderStyle(Format::BorderThin);greyRightStyle.setRightBorderColor(borderColor);greyRightStyle.setBottomBorderStyle(Format::BorderThin);greyRightStyle.setBottomBorderColor(borderColor);int rownum = 3;for (int day = 1; day <= 31; ++day) {QDate date(today.year(), month, day);if (!date.isValid())break;xlsx.setRowHeight(rownum, 100);int dow = date.dayOfWeek();int colnum = dow * 2 - 1;if (dow <= 5) {xlsx.write(rownum, colnum, day, workdayLeftStyle);xlsx.write(rownum, colnum + 1, QVariant(), workdayRightStyle);} else {xlsx.write(rownum, colnum, day, weekendLeftStyle);xlsx.write(rownum, colnum + 1, QVariant(), weekendRightStyle);}if (day == 1 && dow != 1) { // First dayfor (int i = 1; i < dow; ++i) {xlsx.write(rownum, i * 2 - 1, QVariant(), greyLeftStyle);xlsx.write(rownum, i * 2, QVariant(), greyRightStyle);}} else if (day == date.daysInMonth() && dow != 7) { // Last dayfor (int i = dow + 1; i <= 7; ++i) {xlsx.write(rownum, i * 2 - 1, QVariant(), greyLeftStyle);xlsx.write(rownum, i * 2, QVariant(), greyRightStyle);}}if (dow == 7)rownum++;}}xlsx.saveAs("Book1.xlsx");// Make sure that read/write works well.Document xlsx2("Book1.xlsx");xlsx2.saveAs("Book2.xlsx");}MainWindow::~MainWindow()
{delete ui;
}
3.常用方法
创建和保存Excel文件:
QXlsx::Document xlsx;
xlsx.write("A1", "Hello");
xlsx.write("B1", "World");
xlsx.saveAs("example.xlsx");
读取单元格数据:
QXlsx::Document xlsx("example.xlsx");
QString cellValue = xlsx.read("A1")->toString();
读取列数据:
QXlsx::Document xlsx("example.xlsx");
QStringList columnValues = xlsx.read("B")->toStringList();
修改单元格数据:
QXlsx::Document xlsx("example.xlsx");
xlsx.write("A2", 123);
xlsx.save();
合并单元格:
QXlsx::Document xlsx("example.xlsx");
xlsx.mergeCells("A1:B1");
xlsx.save();
设置单元格格式:
QXlsx::Document xlsx("example.xlsx");
xlsx.setColumnWidth(1, 30);
xlsx.setCellFont(1, 1, QFont("Arial", 12, QFont::Bold));
xlsx.save();
操作工作表:
QXlsx::Document xlsx("example.xlsx");
xlsx.selectSheet("Sheet2"); // 选中某个工作表
xlsx.addSheet("NewSheet"); // 添加一个新的工作表
xlsx.deleteSheet("Sheet1"); // 删除指定工作表
xlsx.save();
插入图片:
QXlsx::Document xlsx("example.xlsx");
QImage image("image.png");
xlsx.insertImage(1, 1, image);
xlsx.save();相关文章:
Qt 使用QtXlsx操作Excel表
1.环境搭建 QtXlsx是一个用于读写Microsoft Excel文件(.xlsx)的Qt库。它提供了一组简单易用的API,可以方便地处理电子表格数据。 Github下载:GitHub - dbzhang800/QtXlsxWriter: .xlsx file reader and writer for Qt5 官方文档…...
canal+es+kibana+springboot
1、环境准备 服务器:Centos7 Jdk版本:1.8 Mysql版本:5.7.44 Canal版本:1.17 Es版本:7.12.1 kibana版本:7.12.1 软件包下载地址:链接:https://pan.baidu.com/s/1jRpCJP0-hr9aI…...
【力扣】面试经典150题——双指针
文章目录 125. 验证回文串392. 判断子序列167. 两数之和 II - 输入有序数组11. 盛最多水的容器15. 三数之和 125. 验证回文串 如果在将所有大写字符转换为小写字符、并移除所有非字母数字字符之后,短语正着读和反着读都一样。则可以认为该短语是一个 回文串 。 字…...
6-8 最宽层次结点数 分数 10
文章目录 1.题目描述2.本题ac答案2.1法一: 代码复用2.2法二: 顺序队列实现层序遍历 3.C层序遍历求最大宽度3.1层序遍历代码3.2求最大宽度 1.题目描述 2.本题ac答案 2.1法一: 代码复用 //二叉树第i层结点个数 int LevelNodeCount(BiTree T, int i) {if (T NULL || i < 1)re…...
Linux学习第28天:Platform设备驱动开发(二): 专注与分散
Linux版本号4.1.15 芯片I.MX6ULL 大叔学Linux 品人间百味 思文短情长 三、硬件原理图分析 四、驱动开发 1、platform设备与驱动程序开发 53 /* 54 * 设备资源信息,也就是 LED0 所使用的所有寄存器 55 */ 56 static str…...
postgresql数组重叠(有共同元素)查询
直接上最终代码: select distinct id from a where string_to_array(in_area,,) && (select ARRAY_AGG( code) from areas where code like 11% or code 100000)::TEXT[] pg语法: 表 9.48显示了可用于数组类型的运算符。 表 9.48。数组运算符…...
ubuntu系统 生成RSA密钥对
在Ubuntu系统上生成密钥对通常指的是生成SSH密钥对,它常用于安全的远程登录、数据通信和其他安全网络操作。以下是如何在Ubuntu系统上生成SSH密钥对的步骤: 打开终端:你可以使用快捷键 Ctrl Alt T 在Ubuntu上打开一个终端窗口。 运行ssh-k…...
【RtpSeqNumOnlyRefFinder】webrtc m98: ManageFrameInternal 的帧决策过程分析
Jitterbuffer(FrameBuffer)需要组帧以后GOP内的参考关系 JeffreyLau 大神分析 了组帧原理而参考关系(RtpFrameReferenceFinder)的生成伴随了帧决策 FrameDecisionFrameDecision 影响力 帧的缓存。调用 OnAssembledFrame 传递已经拿到的RtpFrameObject 那么,RtpFrameObject…...
centos系统源码编译安装nginx,并编写服务脚本
1.安装编译所需的依赖项: yum install -y gcc pcre-devel openssl-devel zlib-devel2.下载 Nginx 源代码: wget http://nginx.org/download/nginx-1.21.3.tar.gz tar -xf nginx-1.21.3.tar.gz cd nginx-1.21.33.配置编译选项并进行编译和安装ÿ…...
2023下半年软考高项答题技巧!
2023下半年软考倒计时最后一天,一些软考高项答题技巧分享! 高项答题技巧 1、综合知识 (1)首先是分析试题的技巧 –先看清楚问题,再看选项; –判断题目到底考察的是什么知识点,排除干扰项。…...
windows server 2016调优
1. 增加TCP连接的最大数量: 在您当前的注册表路径(HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters)中的右侧窗格,右击空白处,选择“新建” -> “DWORD (32位) 值”。为新的值命名为TcpNu…...
Qt 插件开发详解
1.简介 Qt插件是一种扩展机制,用于将应用程序的功能模块化,并且可以在运行时动态加载和卸载。Qt框架为插件提供了一套标准的接口和管理机制,使得插件的使用和集成变得简单和灵活,通过插件机制,可以将应用程序的功能划…...
vue需求:实现签章/签字在页面上自由定位的功能(本质:元素在页面上的拖拽)
目录 第一章 效果展示 第二章 了解工具 2.1 draggable 2.1.1 了解draggable 2.1.2 draggable方法 2.1.3 利用例子理解方法 第三章 效果实现 3.1 实现思路 3.2 代码实现 3.2.1 涉及到的点 3.2.2 源代 第一章 效果展示 效果描述:通过点击左边栏的签名和…...
【深度学习基础】Pytorch框架CV开发(1)基础铺垫
📢:如果你也对机器人、人工智能感兴趣,看来我们志同道合✨ 📢:不妨浏览一下我的博客主页【https://blog.csdn.net/weixin_51244852】 📢:文章若有幸对你有帮助,可点赞 👍…...
uniapp原生插件之安卓热敏打印机打印插件
插件介绍 安卓热敏打印机打印插件,自动授权,打印机连接监听,打印文本,条形码,二维码,切纸,打印机状态,打印结果查询等 插件地址 安卓热敏打印机打印插件 - DCloud 插件市场 超级…...
巴菲特:卖比亚迪有助于资金配置
巴菲特表示,未来可能会有更多银行倒闭,但储户不必担心,他警告说,陷入困境的银行股不是价值投资,因为即使政府采取行动保护储户,股东的权益也会受到损失。他称,将加大对日本综合商社的投资&#…...
香港服务器有哪些特点
香港服务器具有以下特点: 速度快:香港服务器地理位置优越,与内地服务器相比,网络延迟更低,访问速度更快。 稳定性高:香港服务器位于全球重要的金融中心,网络环境稳定,服务器稳定性高…...
Leetcode76最小覆盖子串
思路:滑动窗口思想 1. 滑动窗口是什么:用一个滑动窗口为覆盖目标子串的字符串 2.怎么移动窗口:当不满足覆盖时右指针移动扩大范围,当覆盖了就移动左指针缩减范围直到再次不覆盖 3. 怎么判断是否覆盖:这里使用两个哈…...
GD32 单片机 硬件I2C死锁解决方法
死锁的复现方式 在I2C恢复函数下个断点(检测到I2C多次超时之后,应该能跳转到I2C恢复函数)使用镊子,将SCL与SDA短接,很快就能看到程序停到恢复函数的断点上,此时再执行恢复函数,看能否正常走出&…...
SPSS两相关样本检验
前言: 本专栏参考教材为《SPSS22.0从入门到精通》,由于软件版本原因,部分内容有所改变,为适应软件版本的变化,特此创作此专栏便于大家学习。本专栏使用软件为:SPSS25.0 本专栏所有的数据文件请点击此链接下…...
边缘计算医疗风险自查APP开发方案
核心目标:在便携设备(智能手表/家用检测仪)部署轻量化疾病预测模型,实现低延迟、隐私安全的实时健康风险评估。 一、技术架构设计 #mermaid-svg-iuNaeeLK2YoFKfao {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg…...
【JVM】- 内存结构
引言 JVM:Java Virtual Machine 定义:Java虚拟机,Java二进制字节码的运行环境好处: 一次编写,到处运行自动内存管理,垃圾回收的功能数组下标越界检查(会抛异常,不会覆盖到其他代码…...
React Native在HarmonyOS 5.0阅读类应用开发中的实践
一、技术选型背景 随着HarmonyOS 5.0对Web兼容层的增强,React Native作为跨平台框架可通过重新编译ArkTS组件实现85%以上的代码复用率。阅读类应用具有UI复杂度低、数据流清晰的特点。 二、核心实现方案 1. 环境配置 (1)使用React Native…...
SpringBoot+uniapp 的 Champion 俱乐部微信小程序设计与实现,论文初版实现
摘要 本论文旨在设计并实现基于 SpringBoot 和 uniapp 的 Champion 俱乐部微信小程序,以满足俱乐部线上活动推广、会员管理、社交互动等需求。通过 SpringBoot 搭建后端服务,提供稳定高效的数据处理与业务逻辑支持;利用 uniapp 实现跨平台前…...
Axios请求超时重发机制
Axios 超时重新请求实现方案 在 Axios 中实现超时重新请求可以通过以下几种方式: 1. 使用拦截器实现自动重试 import axios from axios;// 创建axios实例 const instance axios.create();// 设置超时时间 instance.defaults.timeout 5000;// 最大重试次数 cons…...
Element Plus 表单(el-form)中关于正整数输入的校验规则
目录 1 单个正整数输入1.1 模板1.2 校验规则 2 两个正整数输入(联动)2.1 模板2.2 校验规则2.3 CSS 1 单个正整数输入 1.1 模板 <el-formref"formRef":model"formData":rules"formRules"label-width"150px"…...
嵌入式学习笔记DAY33(网络编程——TCP)
一、网络架构 C/S (client/server 客户端/服务器):由客户端和服务器端两个部分组成。客户端通常是用户使用的应用程序,负责提供用户界面和交互逻辑 ,接收用户输入,向服务器发送请求,并展示服务…...
【VLNs篇】07:NavRL—在动态环境中学习安全飞行
项目内容论文标题NavRL: 在动态环境中学习安全飞行 (NavRL: Learning Safe Flight in Dynamic Environments)核心问题解决无人机在包含静态和动态障碍物的复杂环境中进行安全、高效自主导航的挑战,克服传统方法和现有强化学习方法的局限性。核心算法基于近端策略优化…...
day36-多路IO复用
一、基本概念 (服务器多客户端模型) 定义:单线程或单进程同时监测若干个文件描述符是否可以执行IO操作的能力 作用:应用程序通常需要处理来自多条事件流中的事件,比如我现在用的电脑,需要同时处理键盘鼠标…...
PHP 8.5 即将发布:管道操作符、强力调试
前不久,PHP宣布了即将在 2025 年 11 月 20 日 正式发布的 PHP 8.5!作为 PHP 语言的又一次重要迭代,PHP 8.5 承诺带来一系列旨在提升代码可读性、健壮性以及开发者效率的改进。而更令人兴奋的是,借助强大的本地开发环境 ServBay&am…...
