当前位置: 首页 > news >正文

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文件&#xff08;.xlsx&#xff09;的Qt库。它提供了一组简单易用的API&#xff0c;可以方便地处理电子表格数据。 Github下载&#xff1a;GitHub - dbzhang800/QtXlsxWriter: .xlsx file reader and writer for Qt5 官方文档…...

canal+es+kibana+springboot

1、环境准备 服务器&#xff1a;Centos7 Jdk版本&#xff1a;1.8 Mysql版本&#xff1a;5.7.44 Canal版本&#xff1a;1.17 Es版本&#xff1a;7.12.1 kibana版本&#xff1a;7.12.1 软件包下载地址&#xff1a;链接&#xff1a;https://pan.baidu.com/s/1jRpCJP0-hr9aI…...

【力扣】面试经典150题——双指针

文章目录 125. 验证回文串392. 判断子序列167. 两数之和 II - 输入有序数组11. 盛最多水的容器15. 三数之和 125. 验证回文串 如果在将所有大写字符转换为小写字符、并移除所有非字母数字字符之后&#xff0c;短语正着读和反着读都一样。则可以认为该短语是一个 回文串 。 字…...

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 * 设备资源信息&#xff0c;也就是 LED0 所使用的所有寄存器 55 */ 56 static str…...

postgresql数组重叠(有共同元素)查询

直接上最终代码&#xff1a; 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语法&#xff1a; 表 9.48显示了可用于数组类型的运算符。 表 9.48。数组运算符…...

ubuntu系统 生成RSA密钥对

在Ubuntu系统上生成密钥对通常指的是生成SSH密钥对&#xff0c;它常用于安全的远程登录、数据通信和其他安全网络操作。以下是如何在Ubuntu系统上生成SSH密钥对的步骤&#xff1a; 打开终端&#xff1a;你可以使用快捷键 Ctrl Alt T 在Ubuntu上打开一个终端窗口。 运行ssh-k…...

【RtpSeqNumOnlyRefFinder】webrtc m98: ManageFrameInternal 的帧决策过程分析

Jitterbuffer(FrameBuffer)需要组帧以后GOP内的参考关系 JeffreyLau 大神分析 了组帧原理而参考关系(RtpFrameReferenceFinder)的生成伴随了帧决策 FrameDecisionFrameDecision 影响力 帧的缓存。调用 OnAssembledFrame 传递已经拿到的RtpFrameObject 那么,RtpFrameObject…...

centos系统源码编译安装nginx,并编写服务脚本

1.安装编译所需的依赖项&#xff1a; yum install -y gcc pcre-devel openssl-devel zlib-devel2.下载 Nginx 源代码&#xff1a; wget http://nginx.org/download/nginx-1.21.3.tar.gz tar -xf nginx-1.21.3.tar.gz cd nginx-1.21.33.配置编译选项并进行编译和安装&#xff…...

2023下半年软考高项答题技巧!

2023下半年软考倒计时最后一天&#xff0c;一些软考高项答题技巧分享&#xff01; 高项答题技巧 1、综合知识 &#xff08;1&#xff09;首先是分析试题的技巧 –先看清楚问题&#xff0c;再看选项&#xff1b; –判断题目到底考察的是什么知识点&#xff0c;排除干扰项。…...

windows server 2016调优

1. 增加TCP连接的最大数量&#xff1a; 在您当前的注册表路径&#xff08;HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters&#xff09;中的右侧窗格&#xff0c;右击空白处&#xff0c;选择“新建” -> “DWORD (32位) 值”。为新的值命名为TcpNu…...

Qt 插件开发详解

1.简介 Qt插件是一种扩展机制&#xff0c;用于将应用程序的功能模块化&#xff0c;并且可以在运行时动态加载和卸载。Qt框架为插件提供了一套标准的接口和管理机制&#xff0c;使得插件的使用和集成变得简单和灵活&#xff0c;通过插件机制&#xff0c;可以将应用程序的功能划…...

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 源代 第一章 效果展示 效果描述&#xff1a;通过点击左边栏的签名和…...

【深度学习基础】Pytorch框架CV开发(1)基础铺垫

&#x1f4e2;&#xff1a;如果你也对机器人、人工智能感兴趣&#xff0c;看来我们志同道合✨ &#x1f4e2;&#xff1a;不妨浏览一下我的博客主页【https://blog.csdn.net/weixin_51244852】 &#x1f4e2;&#xff1a;文章若有幸对你有帮助&#xff0c;可点赞 &#x1f44d;…...

uniapp原生插件之安卓热敏打印机打印插件

插件介绍 安卓热敏打印机打印插件&#xff0c;自动授权&#xff0c;打印机连接监听&#xff0c;打印文本&#xff0c;条形码&#xff0c;二维码&#xff0c;切纸&#xff0c;打印机状态&#xff0c;打印结果查询等 插件地址 安卓热敏打印机打印插件 - DCloud 插件市场 超级…...

巴菲特:卖比亚迪有助于资金配置

巴菲特表示&#xff0c;未来可能会有更多银行倒闭&#xff0c;但储户不必担心&#xff0c;他警告说&#xff0c;陷入困境的银行股不是价值投资&#xff0c;因为即使政府采取行动保护储户&#xff0c;股东的权益也会受到损失。他称&#xff0c;将加大对日本综合商社的投资&#…...

香港服务器有哪些特点

香港服务器具有以下特点&#xff1a; 速度快&#xff1a;香港服务器地理位置优越&#xff0c;与内地服务器相比&#xff0c;网络延迟更低&#xff0c;访问速度更快。 稳定性高&#xff1a;香港服务器位于全球重要的金融中心&#xff0c;网络环境稳定&#xff0c;服务器稳定性高…...

Leetcode76最小覆盖子串

思路&#xff1a;滑动窗口思想 1. 滑动窗口是什么&#xff1a;用一个滑动窗口为覆盖目标子串的字符串 2.怎么移动窗口&#xff1a;当不满足覆盖时右指针移动扩大范围&#xff0c;当覆盖了就移动左指针缩减范围直到再次不覆盖 3. 怎么判断是否覆盖&#xff1a;这里使用两个哈…...

GD32 单片机 硬件I2C死锁解决方法

死锁的复现方式 在I2C恢复函数下个断点&#xff08;检测到I2C多次超时之后&#xff0c;应该能跳转到I2C恢复函数&#xff09;使用镊子&#xff0c;将SCL与SDA短接&#xff0c;很快就能看到程序停到恢复函数的断点上&#xff0c;此时再执行恢复函数&#xff0c;看能否正常走出&…...

SPSS两相关样本检验

前言&#xff1a; 本专栏参考教材为《SPSS22.0从入门到精通》&#xff0c;由于软件版本原因&#xff0c;部分内容有所改变&#xff0c;为适应软件版本的变化&#xff0c;特此创作此专栏便于大家学习。本专栏使用软件为&#xff1a;SPSS25.0 本专栏所有的数据文件请点击此链接下…...

利用最小二乘法找圆心和半径

#include <iostream> #include <vector> #include <cmath> #include <Eigen/Dense> // 需安装Eigen库用于矩阵运算 // 定义点结构 struct Point { double x, y; Point(double x_, double y_) : x(x_), y(y_) {} }; // 最小二乘法求圆心和半径 …...

day52 ResNet18 CBAM

在深度学习的旅程中&#xff0c;我们不断探索如何提升模型的性能。今天&#xff0c;我将分享我在 ResNet18 模型中插入 CBAM&#xff08;Convolutional Block Attention Module&#xff09;模块&#xff0c;并采用分阶段微调策略的实践过程。通过这个过程&#xff0c;我不仅提升…...

visual studio 2022更改主题为深色

visual studio 2022更改主题为深色 点击visual studio 上方的 工具-> 选项 在选项窗口中&#xff0c;选择 环境 -> 常规 &#xff0c;将其中的颜色主题改成深色 点击确定&#xff0c;更改完成...

在鸿蒙HarmonyOS 5中使用DevEco Studio实现录音机应用

1. 项目配置与权限设置 1.1 配置module.json5 {"module": {"requestPermissions": [{"name": "ohos.permission.MICROPHONE","reason": "录音需要麦克风权限"},{"name": "ohos.permission.WRITE…...

C++八股 —— 单例模式

文章目录 1. 基本概念2. 设计要点3. 实现方式4. 详解懒汉模式 1. 基本概念 线程安全&#xff08;Thread Safety&#xff09; 线程安全是指在多线程环境下&#xff0c;某个函数、类或代码片段能够被多个线程同时调用时&#xff0c;仍能保证数据的一致性和逻辑的正确性&#xf…...

高效线程安全的单例模式:Python 中的懒加载与自定义初始化参数

高效线程安全的单例模式:Python 中的懒加载与自定义初始化参数 在软件开发中,单例模式(Singleton Pattern)是一种常见的设计模式,确保一个类仅有一个实例,并提供一个全局访问点。在多线程环境下,实现单例模式时需要注意线程安全问题,以防止多个线程同时创建实例,导致…...

Bean 作用域有哪些?如何答出技术深度?

导语&#xff1a; Spring 面试绕不开 Bean 的作用域问题&#xff0c;这是面试官考察候选人对 Spring 框架理解深度的常见方式。本文将围绕“Spring 中的 Bean 作用域”展开&#xff0c;结合典型面试题及实战场景&#xff0c;帮你厘清重点&#xff0c;打破模板式回答&#xff0c…...

【LeetCode】算法详解#6 ---除自身以外数组的乘积

1.题目介绍 给定一个整数数组 nums&#xff0c;返回 数组 answer &#xff0c;其中 answer[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积 。 题目数据 保证 数组 nums之中任意元素的全部前缀元素和后缀的乘积都在 32 位 整数范围内。 请 不要使用除法&#xff0c;且在 O…...

二维FDTD算法仿真

二维FDTD算法仿真&#xff0c;并带完全匹配层&#xff0c;输入波形为高斯波、平面波 FDTD_二维/FDTD.zip , 6075 FDTD_二维/FDTD_31.m , 1029 FDTD_二维/FDTD_32.m , 2806 FDTD_二维/FDTD_33.m , 3782 FDTD_二维/FDTD_34.m , 4182 FDTD_二维/FDTD_35.m , 4793...

[USACO23FEB] Bakery S

题目描述 Bessie 开了一家面包店! 在她的面包店里&#xff0c;Bessie 有一个烤箱&#xff0c;可以在 t C t_C tC​ 的时间内生产一块饼干或在 t M t_M tM​ 单位时间内生产一块松糕。 ( 1 ≤ t C , t M ≤ 10 9 ) (1 \le t_C,t_M \le 10^9) (1≤tC​,tM​≤109)。由于空间…...