Qt 使用QtXlsx操作Excel表

news2025/7/16 17:22:51

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_NAMESPACE

MainWindow::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 row
        Format 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 titles
        for (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 day
                for (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 day
                for (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();

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1157683.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Fetch库

scalaimport com.github.katongli.http.crawler.Fetchval fetchFetch()fetch.setProxyHost("jshk.com.cn//aa")fetch.setProxyPort(0126)val responsefetch(url)val imagesresponse.images//你可以使用println将获取的图片打印出来println(images) 解释&#xff1a;…

markdown增加目录索引,实现点击目录跳转到对应的内容目录标题

文章目录 1. 教程1.1 首先正常编写文章例如如下1.2 原理 2. 示例文件2.1 标题12.1.1 小标题12.1.1.1 小小标题12.1.1.2 小小标题2 2.1.2 小标题2 1. 教程 1.1 首先正常编写文章例如如下 如果使用的是typora则可以直接点击段落-》内容目录&#xff1b;则会自动生成目录 1.2 原理…

Linux之J2EE项目部署与发布(Linux版本)

&#x1f389;&#x1f389;欢迎来到我的CSDN主页&#xff01;&#x1f389;&#x1f389; &#x1f3c5;我是君易--鑨&#xff0c;一个在CSDN分享笔记的博主。&#x1f4da;&#x1f4da; &#x1f31f;推荐给大家我的博客专栏《LInux实战开发》。&#x1f3af;&#x1f3af; …

高阶JAVA篇-深入了解字符集

&#x1f525;博客主页&#xff1a; 小扳_-CSDN博客 ❤感谢大家点赞&#x1f44d;收藏⭐评论✍ 文章目录 1.0 字符集的说明 1.1 ASCII 字符集 1.2 GBK 字符集 1.3 UTF-8字符集 2.0 字符集的编码与解码 2.1 编码提供了常见的方法 2.2 解码提供了常见的方法 1.0 字符集的说明 字…

【爬虫系统设计系列】模板爬虫的动态配置策略设计与实现

文章目录 1. 写在前面2. 页面配置规划3. 制定模板格式4. 模板引擎实现5. 模板爬虫优势 1. 写在前面 作为一名爬虫开发者来说&#xff0c;涉及数据采集和爬虫开发时&#xff0c;往往都面临着各种挑战。包括技术复杂性、维护成本以及数据源结构的不断变化 早期我们对爬虫开发方式…

【蓝桥杯选拔赛真题45】python调和级数 青少年组蓝桥杯python 选拔赛STEMA比赛真题解析

目录 python调和级数 一、题目要求 1、编程实现 2、输入输出 二、算法分析

众和策略:微软大动作

当地时间周二&#xff0c;美股首要指数全线收涨。但从月度数据来看&#xff0c;美股首要指数录得“三连跌”&#xff0c;10月份&#xff0c;道指跌1.36%&#xff0c;标普500指数跌2.2%&#xff0c;纳指跌2.78%。其间&#xff0c;标普和道指均为2020年3月以来初次呈现三个月连跌…

不容错过的设计软件,产品设计必看

一.图片绘制和处理工具 产品设计很多时候是需要完成产品效果图的相关工作&#xff0c;所以一些图片绘制和处理工具&#xff0c;二维或三维的都需要了解一下。 2D软件&#xff1a; 1、photoshop(PS) AdobePhotoshop&#xff0c;简称“PS这是Adobe开发并发布的一款图片处理软…

innovus: set_ccopt_property的基本用法

我正在「拾陆楼」和朋友们讨论有趣的话题&#xff0c;你⼀起来吧&#xff1f; 拾陆楼知识星球入口 clock route clock route的net type分为三种&#xff0c;分别是root、trunk和leaf&#xff0c;其中root是指fanout超过routing_top_fanout_count约束的net&#xff0c;leaf是指…

mysql:B+树/事务

B树 : 为了数据库量身定做的数据结构 我们当前这里的讨论都是围绕 mysql 的 innodb 这个存储引擎来讨论的 其他存储引擎可能会用到hash 作为索引,此时就只能应对这种精准匹配的情况了 要了解 B树 我们先了解 B树, B树 是 B树 的改进 B树 有时候会写作 B-树 (这里的" -…

GB28181协议怎样执行保活命令

前言 GB28181协议是视频监控领域的国家标准&#xff0c;本文将解析如何在FFmpeg中增加对GB28181协议的支持&#xff0c;使其可以与支持GB28181协议的设备进行通信与控制&#xff0c;实现设备的注册、保活以及流媒体的传输。 背景介绍 GB28181协议指的是国家标准GB/T 28181—…

自学SLAM(6)相机与图像实践:OpenCV处理图像与图像拼接(点云)

前言 如果写过SLAM14讲第一次的作业&#xff0c;或者看过我之前的运行ORB_SLAM2教程应该都安装过OpenCV了&#xff0c;如果没有安装&#xff0c;没关系&#xff0c;可以看我之前的博客&#xff0c;里面有如何安装OpenCV。 链接: 运行ORB-SLAM2&#xff08;含OpenCV的安装&…

加强城市内涝积水监测系统建设,提高城市预警功能

近年来&#xff0c;随着城市化进程的不断加快&#xff0c;城市内涝问题愈发凸显&#xff0c;给城市的生命线带来了严重威胁。为了及时掌握城市内涝的情况&#xff0c;保障城市的正常运行&#xff0c;各地纷纷建立了城市内涝监测系统。城市内涝监测系统作为城市生命线的重要组成…

Redis与MySQL的数据情感:延迟双删的秘密揭示

Redis与MySQL的数据情感&#xff1a;延迟双删的秘密揭示 前言第一&#xff1a;mysql与redis数据不一致问题第二&#xff1a;为什么需要双删第三&#xff1a;如何实现延迟双删 前言 在现代应用程序中&#xff0c;MySQL 和 Redis 是两种常用的数据存储解决方案。然而&#xff0c…

金蝶云星空自定义校验器和使用

文章目录 金蝶云星空自定义校验器和使用 金蝶云星空自定义校验器和使用 1、创建类&#xff0c;并继承抽象接口 using Kingdee.BOS.Core; using Kingdee.BOS.Core.Validation; using System;namespace mm.K3.SCM.App.Service.PlugIn.SC.Validator {public class AfterOrderChe…

Python使用got库如何写一个爬虫代码?

got库是一个Python的HTTP库&#xff0c;可以用于爬取网页数据。它提供了简单易用的API&#xff0c;支持异步请求和爬虫IP设置等功能。使用got库进行爬虫开发&#xff0c;可以快速地获取所需数据。下面是使用got库进行爬虫的基本步骤&#xff1a; 1、安装got库&#xff1a;可以使…

如何正确学习中国传统画——画家蒋旗

艺术简介 蒋旗&#xff1a; 师从张建中、张立辰 授教于郭石夫、陈曦林、薛永年、张旭光、乔森、于光华、高卉民、潘晓云 中国书画院院士 清华美院大写意花鸟画高研班助教导师 安徽美术家协会会员 泗县美术家协会副主席 青藤画社社长。 在艺术多元发展的当下&#xff0c…

【23真题】Top3简单专业课似双非!

今天分享的是23年复旦大学957的信号与系统试题及解析。 本套试卷难度分析&#xff1a;这套卷子平均分为120左右&#xff0c;最高分145分。22年复旦大学957信号与系统&#xff0c;我也发布过&#xff0c;若有需要戳这里自取&#xff01;本套试题内容难度中等偏下&#xff0c;说…

AutoGen完整教程和加载本地LLM示例

Autogen是一个卓越的人工智能系统&#xff0c;它可以创建多个人工智能代理&#xff0c;这些代理能够协作完成任务&#xff0c;包括自动生成代码&#xff0c;并有效地执行任务。 在本文中&#xff0c;我们将深入探讨Autogen&#xff0c;并介绍如何让AutoGen使用本地的LLM Auto…

山西电力市场日前价格预测【2023-11-02】

日前价格预测 预测说明&#xff1a; 如上图所示&#xff0c;预测明日&#xff08;2023-11-02&#xff09;山西电力市场全天平均日前电价为151.67元/MWh。其中&#xff0c;最高日前电价为280.23元/MWh&#xff0c;预计出现在22:15。最低日前电价为0.00元/MWh&#xff0c;预计出…