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

QExcel 保存数据 (QtXlsxWriter库)

  QtXlsxWriter 是一个用于在 Qt 应用程序中创建和操作 Excel XLSX 文件的库。它提供了一个简单的 API,使开发者能够轻松地生成和修改 Excel 文件,而无需依赖 Microsoft Excel 或其他外部应用程序。支持初始化、写文件、读文件、格式设置、合并单元格、加粗、字体颜色、字体大小、水平居中、下划线、背景色、边框样式、边框颜色、上下左右边框以及颜色、效果演示、设置宽度、设置高度、读取格式、源文件代码
主要特性

1. 创建和写入 XLSX 文件:

    可以创建新的 Excel 文件并向其中写入数据,包括文本、数字、日期等。

2. 支持多种数据类型:

    支持写入多种数据类型,如字符串、整数、浮点数、布尔值等。

3. 格式化单元格:

    提供丰富的单元格格式化选项,包括字体、颜色、边框、对齐方式等。

4. 支持公式:

    可以在单元格中写入 Excel 公式,支持基本的数学和逻辑运算。

5. 图表支持:

    可以在 Excel 文件中插入图表,以可视化数据。

    读取和修改现有文件:

    除了创建新文件外,还可以读取和修改现有的 XLSX 文件。

7. 跨平台:

    作为 Qt 的一部分,QtXlsxWriter 可以在多个操作系统上使用,包括 Windows、Linux 和 macOS。

转载请附上文章出处与本文链接。

QExcel 保存数据 (QtXlsxWriter库)目录

1 初始化

2 写文件

3 读文件

4 格式设置

4.1 合并单元格

4.2 加粗

4.3 字体颜色

4.4 字体大小

4.5 水平居中

4.6 下划线

4.7 背景色

4.8 边框样式

4.9 边框颜色

4.10 上下左右边框以及颜色

4.11 效果

4.12 设置宽度

4.13 设置高度

5 读取格式

6 .h文件

7 .cpp文件


1 初始化

        需要引入xlsxdocument 头文件

#pragma once#include <QtWidgets/QMainWindow>
#include "ui_QExcelTest.h"#include <QDebug>
#include "xlsxdocument.h"
#pragma execution_character_set("utf-8")class QExcelTest : public QMainWindow
{Q_OBJECTpublic:QExcelTest(QWidget *parent = nullptr);~QExcelTest();private:void readXlsx(QString strPath);void writeXlsx(QString strPath);private:Ui::QExcelTestClass ui;QXlsx::Document *m_xlsx = nullptr;QString strFilePath = "example.xlsx";QStringList msExpTitle;
};
QExcelTest::QExcelTest(QWidget *parent): QMainWindow(parent)
{ui.setupUi(this);m_xlsx = new QXlsx::Document;msExpTitle.clear();msExpTitle << "A" << "B" << "C" << "D" << "E" << "F" << "G";writeXlsx(strFilePath);}QExcelTest::~QExcelTest()
{//if (m_xlsx != nullptr)//{//	delete m_xlsx;//	m_xlsx = nullptr;//}
}

2 写文件

void QExcelTest::writeXlsx(QString strPath)
{QStringList tempTitle;QStringList tempUserData;tempTitle << "id" << "name" << "type" << "time" << "remark";tempUserData << "王霸天" << "管理员" << "2024-10-17" << "null";for (int i = 0; i < tempTitle.size(); i++){m_xlsx->write(msExpTitle[i] + QString::number(1), tempTitle[i]);}for (int i = 0; i < 10; i++){m_xlsx->write(msExpTitle[0] + QString::number(i + 2), QString::number(i));m_xlsx->write(msExpTitle[1] + QString::number(i + 2), tempUserData[0]);m_xlsx->write(msExpTitle[2] + QString::number(i + 2), tempUserData[1]);m_xlsx->write(msExpTitle[3] + QString::number(i + 2), tempUserData[2]);m_xlsx->write(msExpTitle[4] + QString::number(i + 2), tempUserData[3]);}// 保存文档if (m_xlsx->saveAs(strPath)){qDebug() << "Excel file created successfully!";}
}

3 读文件

void QExcelTest::readXlsx(QString strPath)
{// 读取数据for (int row = 1; row <= 10; ++row) { // 假设我们要读取前 10 行QString id = m_xlsx->read("A" + QString::number(row)).toString();QString name = m_xlsx->read("B" + QString::number(row)).toString();QString type = m_xlsx->read("C" + QString::number(row)).toString();QString time = m_xlsx->read("D" + QString::number(row)).toString();QString remark = m_xlsx->read("E" + QString::number(row)).toString();// 输出读取的数据qDebug() << "Row" << row << ":"<< "ID:" << id<< "Name:" << name<< "Type:" << type<< "Time:" << time<< "Remark:" << remark;}
}

4 格式设置

4.1 合并单元格

	// 合并单元格m_xlsx->mergeCells("A4:E4"); // 合并 A4 到 E4 的单元格m_xlsx->write("A4", "Merged Cell", headerFormat); // 在合并的单元格中写入内容

4.2 加粗

setFontBold(true); // 加粗

4.3 字体颜色

setFontColor(QColor(Qt::blue)); // 字体颜色

4.4 字体大小

setFontSize(14);

4.5 水平居中

setHorizontalAlignment(QXlsx::Format::AlignHCenter);

4.6 下划线

setFontUnderline(QXlsx::Format::FontUnderlineSingle); // 下划线

4.7 背景色

setPatternBackgroundColor(QColor(Qt::yellow)); // 背景色

4.8 边框样式

setBorderStyle(QXlsx::Format::BorderThin); // 边框样式

4.9 边框颜色

setBorderColor(QColor(Qt::black)); // 边框颜色

4.10 上下左右边框以及颜色

	// 设置上下左右框线//cellFormat.setLeftBorderStyle(QXlsx::Format::BorderThin);//cellFormat.setLeftBorderColor(QColor(Qt::black));//cellFormat.setRightBorderStyle(QXlsx::Format::BorderThin);//cellFormat.setRightBorderColor(QColor(Qt::black));//cellFormat.setTopBorderStyle(QXlsx::Format::BorderThin);//cellFormat.setTopBorderColor(QColor(Qt::black));//cellFormat.setBottomBorderStyle(QXlsx::Format::BorderThin);//cellFormat.setBottomBorderColor(QColor(Qt::black));

4.11 效果

4.12 设置宽度

m_xlsx->setColumnWidth(1, 20); // 设置第一列(A列)宽度为 20

4.13 设置高度

m_xlsx->setRowHeight(1, 25); // 设置第一行(1行)高度为 25

5 读取格式

void QExcelTest::readFormatXlsx()
{// 读取数据和样式for (int row = 1; row <= 10; ++row) { // 假设读取前 10 行for (int col = 1; col <= 5; ++col) { // 假设读取前 5 列QString cellValue = m_xlsx->read(row, col).toString();QXlsx::Format cellFormat = m_xlsx->columnFormat(row);// 输出单元格的值qDebug() << "Cell (" << row << "," << col << "):" << cellValue;// 输出样式信息if (cellFormat.fontBold()) {qDebug() << "  Font Bold: Yes";}if (cellFormat.fontColor() != QColor(Qt::black)) {qDebug() << "  Font Color:" << cellFormat.fontColor();}if (cellFormat.fontUnderline() == QXlsx::Format::FontUnderlineSingle) {qDebug() << "  Underline: Yes";}if (cellFormat.patternBackgroundColor() != QColor(Qt::white)) {qDebug() << "  Background Color:" << cellFormat.patternBackgroundColor();}// 读取边框样式if (cellFormat.leftBorderStyle() != QXlsx::Format::BorderNone) {qDebug() << "  Left Border Style:" << cellFormat.leftBorderStyle();}if (cellFormat.rightBorderStyle() != QXlsx::Format::BorderNone) {qDebug() << "  Right Border Style:" << cellFormat.rightBorderStyle();}if (cellFormat.topBorderStyle() != QXlsx::Format::BorderNone) {qDebug() << "  Top Border Style:" << cellFormat.topBorderStyle();}if (cellFormat.bottomBorderStyle() != QXlsx::Format::BorderNone) {qDebug() << "  Bottom Border Style:" << cellFormat.bottomBorderStyle();}}}
}
Cell ( 1 , 1 ): "ID"Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 1 , 2 ): "Name"Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 1 , 3 ): "Type"Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 1 , 4 ): "Date"Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 1 , 5 ): "Remark"Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 2 , 1 ): "1"Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 2 , 2 ): "Alice"Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 2 , 3 ): "Admin"Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 2 , 4 ): "2024-10-17"Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 2 , 5 ): "No remarks"Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 3 , 1 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 3 , 2 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 3 , 3 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 3 , 4 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 3 , 5 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 4 , 1 ): "Merged Cell"Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 4 , 2 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 4 , 3 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 4 , 4 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 4 , 5 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 5 , 1 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 5 , 2 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 5 , 3 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 5 , 4 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 5 , 5 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 6 , 1 ): "ID"Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 6 , 2 ): "Name"Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 6 , 3 ): "Type"Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 6 , 4 ): "Date"Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 6 , 5 ): "Remark"Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 7 , 1 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 7 , 2 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 7 , 3 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 7 , 4 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 7 , 5 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 8 , 1 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 8 , 2 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 8 , 3 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 8 , 4 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 8 , 5 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 9 , 1 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 9 , 2 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 9 , 3 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 9 , 4 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 9 , 5 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 10 , 1 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 10 , 2 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 10 , 3 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 10 , 4 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)
Cell ( 10 , 5 ): ""Font Color: QColor(Invalid)Background Color: QColor(Invalid)

6 .h文件

#pragma once#include <QtWidgets/QMainWindow>
#include "ui_QExcelTest.h"#include <QDebug>
#include "xlsxdocument.h"
#pragma execution_character_set("utf-8")class QExcelTest : public QMainWindow
{Q_OBJECTpublic:QExcelTest(QWidget *parent = nullptr);~QExcelTest();private:void readXlsx(QString strPath);void writeXlsx(QString strPath);void writeFormatXlsx();private:Ui::QExcelTestClass ui;QXlsx::Document *m_xlsx = nullptr;QString strFilePath = "example.xlsx";QStringList msExpTitle;
};

7 .cpp文件

#include "QExcelTest.h"QExcelTest::QExcelTest(QWidget *parent): QMainWindow(parent)
{ui.setupUi(this);m_xlsx = new QXlsx::Document(strFilePath);msExpTitle.clear();msExpTitle << "A" << "B" << "C" << "D" << "E" << "F" << "G";//writeXlsx("");writeFormatXlsx();//readXlsx("");
}QExcelTest::~QExcelTest()
{//if (m_xlsx != nullptr)//{//	delete m_xlsx;//	m_xlsx = nullptr;//}
}void QExcelTest::readXlsx(QString strPath)
{// 读取数据for (int row = 1; row <= 10; ++row) { // 假设我们要读取前 10 行QString id = m_xlsx->read("A" + QString::number(row)).toString();QString name = m_xlsx->read("B" + QString::number(row)).toString();QString type = m_xlsx->read("C" + QString::number(row)).toString();QString time = m_xlsx->read("D" + QString::number(row)).toString();QString remark = m_xlsx->read("E" + QString::number(row)).toString();// 输出读取的数据qDebug() << "Row" << row << ":"<< "ID:" << id<< "Name:" << name<< "Type:" << type<< "Time:" << time<< "Remark:" << remark;}
}void QExcelTest::writeXlsx(QString strPath)
{QStringList tempTitle;QStringList tempUserData;tempTitle << "id" << "name" << "type" << "time" << "remark";tempUserData << "王霸天" << "管理员" << "2024-10-17" << "null";for (int i = 0; i < tempTitle.size(); i++){m_xlsx->write(msExpTitle[i] + QString::number(1), tempTitle[i]);}for (int i = 0; i < 10; i++){m_xlsx->write(msExpTitle[0] + QString::number(i + 2), QString::number(i));m_xlsx->write(msExpTitle[1] + QString::number(i + 2), tempUserData[0]);m_xlsx->write(msExpTitle[2] + QString::number(i + 2), tempUserData[1]);m_xlsx->write(msExpTitle[3] + QString::number(i + 2), tempUserData[2]);m_xlsx->write(msExpTitle[4] + QString::number(i + 2), tempUserData[3]);}// 保存文档if (m_xlsx->saveAs(strPath)){qDebug() << "Excel file created successfully!";}
}void QExcelTest::writeFormatXlsx()
{// 创建格式对象QXlsx::Format headerFormat;headerFormat.setFontBold(true); // 加粗headerFormat.setFontColor(QColor(Qt::blue)); // 字体颜色headerFormat.setFontSize(14); // 字体大小headerFormat.setHorizontalAlignment(QXlsx::Format::AlignHCenter); // 水平居中// 设置单元格样式m_xlsx->write("A1", "ID", headerFormat);m_xlsx->write("B1", "Name", headerFormat);m_xlsx->write("C1", "Type", headerFormat);m_xlsx->write("D1", "Date", headerFormat);m_xlsx->write("E1", "Remark", headerFormat);// 设置数据格式QXlsx::Format dataFormat;dataFormat.setFontBold(false); // 不加粗dataFormat.setFontColor(QColor(Qt::black)); // 字体颜色dataFormat.setFontSize(12); // 字体大小// 写入数据m_xlsx->write("A2", 1, dataFormat);m_xlsx->write("B2", "Alice", dataFormat);m_xlsx->write("C2", "Admin", dataFormat);m_xlsx->write("D2", "2024-10-17", dataFormat);m_xlsx->write("E2", "No remarks", dataFormat);// 合并单元格m_xlsx->mergeCells("A4:E4"); // 合并 A4 到 E4 的单元格m_xlsx->write("A4", "Merged Cell", headerFormat); // 在合并的单元格中写入内容// 创建格式对象QXlsx::Format cellFormat;cellFormat.setFontBold(true); // 加粗cellFormat.setFontColor(QColor(Qt::black)); // 字体颜色cellFormat.setFontSize(12); // 字体大小cellFormat.setFontUnderline(QXlsx::Format::FontUnderlineSingle); // 下划线cellFormat.setPatternBackgroundColor(QColor(Qt::yellow)); // 背景色cellFormat.setBorderStyle(QXlsx::Format::BorderThin); // 边框样式cellFormat.setBorderColor(QColor(Qt::black)); // 边框颜色// 设置上下左右框线//cellFormat.setLeftBorderStyle(QXlsx::Format::BorderThin);//cellFormat.setLeftBorderColor(QColor(Qt::black));//cellFormat.setRightBorderStyle(QXlsx::Format::BorderThin);//cellFormat.setRightBorderColor(QColor(Qt::black));//cellFormat.setTopBorderStyle(QXlsx::Format::BorderThin);//cellFormat.setTopBorderColor(QColor(Qt::black));//cellFormat.setBottomBorderStyle(QXlsx::Format::BorderThin);//cellFormat.setBottomBorderColor(QColor(Qt::black));// 写入数据并应用格式m_xlsx->write("A6", "ID", cellFormat);m_xlsx->write("B6", "Name", cellFormat);m_xlsx->write("C6", "Type", cellFormat);m_xlsx->write("D6", "Date", cellFormat);m_xlsx->write("E6", "Remark", cellFormat);设置合并单元格的格式//QXlsx::Format mergedFormat;//mergedFormat.setFontBold(true);//mergedFormat.setFontColor(QColor(Qt::red));//mergedFormat.setFontSize(16);//mergedFormat.setHorizontalAlignment(QXlsx::Format::AlignHCenter);//m_xlsx->setFormat("A4", mergedFormat); // 设置合并单元格的格式// 保存文档if (m_xlsx->saveAs(strFilePath)) {qDebug() << "Excel file created successfully!";}
}

8 相关文章

QExcel 保存数据 (QtXlsxWriter库 编译)-CSDN博客


http://www.mrgr.cn/news/53460.html

相关文章:

  • Redis2
  • Scala的sortedWith
  • Java集合常见知识总结(中)
  • 从开发板传送文件回本地
  • 【在Linux世界中追寻伟大的One Piece】应用层自定义协议|序列化
  • 19.面试算法-树的深度优先遍(一)
  • 008、相交链表
  • (01)fastapi的基础学习——开启学习之路
  • JavaScript (基础)
  • 【力扣打卡系列】滑动窗口与双指针(两数之和)
  • 优化漏洞扫描流程以保障企业数字化业务安全
  • 【AI学习】Mamba学习(八):HiPPO通用框架定义和方法
  • 小白学电路之电流镜仿真
  • OpenLayers:用于在 web 应用程序中创建互动地图
  • 目标检测最新SOTA模型D-FINE
  • 【分布式微服务云原生】《Redis 分布式锁的挑战与解决方案及 RedLock 的强大魅力》
  • UG NX12.0建模入门笔记:1.1 UG界面编辑
  • 【Gitee版】一篇教你如何快速入门git(详解)
  • Android 下通过触发 SIGTRAP 信号实现反调试
  • Broker 模式