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 本专栏所有的数据文件请点击此链接下…...

业务系统对接大模型的基础方案:架构设计与关键步骤
业务系统对接大模型:架构设计与关键步骤 在当今数字化转型的浪潮中,大语言模型(LLM)已成为企业提升业务效率和创新能力的关键技术之一。将大模型集成到业务系统中,不仅可以优化用户体验,还能为业务决策提供…...
云原生核心技术 (7/12): K8s 核心概念白话解读(上):Pod 和 Deployment 究竟是什么?
大家好,欢迎来到《云原生核心技术》系列的第七篇! 在上一篇,我们成功地使用 Minikube 或 kind 在自己的电脑上搭建起了一个迷你但功能完备的 Kubernetes 集群。现在,我们就像一个拥有了一块崭新数字土地的农场主,是时…...

Cinnamon修改面板小工具图标
Cinnamon开始菜单-CSDN博客 设置模块都是做好的,比GNOME简单得多! 在 applet.js 里增加 const Settings imports.ui.settings;this.settings new Settings.AppletSettings(this, HTYMenusonichy, instance_id); this.settings.bind(menu-icon, menu…...
linux 下常用变更-8
1、删除普通用户 查询用户初始UID和GIDls -l /home/ ###家目录中查看UID cat /etc/group ###此文件查看GID删除用户1.编辑文件 /etc/passwd 找到对应的行,YW343:x:0:0::/home/YW343:/bin/bash 2.将标红的位置修改为用户对应初始UID和GID: YW3…...
【Web 进阶篇】优雅的接口设计:统一响应、全局异常处理与参数校验
系列回顾: 在上一篇中,我们成功地为应用集成了数据库,并使用 Spring Data JPA 实现了基本的 CRUD API。我们的应用现在能“记忆”数据了!但是,如果你仔细审视那些 API,会发现它们还很“粗糙”:有…...

企业如何增强终端安全?
在数字化转型加速的今天,企业的业务运行越来越依赖于终端设备。从员工的笔记本电脑、智能手机,到工厂里的物联网设备、智能传感器,这些终端构成了企业与外部世界连接的 “神经末梢”。然而,随着远程办公的常态化和设备接入的爆炸式…...

佰力博科技与您探讨热释电测量的几种方法
热释电的测量主要涉及热释电系数的测定,这是表征热释电材料性能的重要参数。热释电系数的测量方法主要包括静态法、动态法和积分电荷法。其中,积分电荷法最为常用,其原理是通过测量在电容器上积累的热释电电荷,从而确定热释电系数…...
Java + Spring Boot + Mybatis 实现批量插入
在 Java 中使用 Spring Boot 和 MyBatis 实现批量插入可以通过以下步骤完成。这里提供两种常用方法:使用 MyBatis 的 <foreach> 标签和批处理模式(ExecutorType.BATCH)。 方法一:使用 XML 的 <foreach> 标签ÿ…...

使用Spring AI和MCP协议构建图片搜索服务
目录 使用Spring AI和MCP协议构建图片搜索服务 引言 技术栈概览 项目架构设计 架构图 服务端开发 1. 创建Spring Boot项目 2. 实现图片搜索工具 3. 配置传输模式 Stdio模式(本地调用) SSE模式(远程调用) 4. 注册工具提…...

解析奥地利 XARION激光超声检测系统:无膜光学麦克风 + 无耦合剂的技术协同优势及多元应用
在工业制造领域,无损检测(NDT)的精度与效率直接影响产品质量与生产安全。奥地利 XARION开发的激光超声精密检测系统,以非接触式光学麦克风技术为核心,打破传统检测瓶颈,为半导体、航空航天、汽车制造等行业提供了高灵敏…...