- Qt读写Excel--QXlsx插入、读取图片6
- @[toc]
- 1、概述
- 2、准备工作歹
- 3、函数说明
- 4、示例代码
- 4.1 .h文件
- 4.2 .cpp文件
- 5、实现效果
- 6、源代码論
| 更多精彩内容 |
|---|
| 个人内容分类汇总 |
-
QXlsx是一个可以读写Excel文件的库。不依赖office以及wps组件,可以在Qt5支持的任何平台上使用;
-
使用方式
- QXlsx可以编译为动态库使用(使用动态库不用每次的编译,也可以让项目代码量更少,不用一打开工程就几十个文件);
- 直接将QXlsx.pri加入代码中使用(我比较推荐直接使用源码,因为QXlsx的注释信息基本在cpp文件中,可以通过阅读源码和注释来学习QXlsx的功能,当然,如果你已经熟悉了QXlsx的使用方式那编译成库使用会更方便,可以使工程的代码量变少);
-
本文中实现的功能:
- 在当前Sheet指定位置插入图片;
- 获取当前Sheet中图片个数;
- 通过索引方式和行列号方式读取当前Sheet中的图片。
| Qt读写Excel–QXlsx基本使用1 |
|---|
| Qt读写Excel–QXlsx编译为静态库2 |
注意:执行了操作要保存才生效。⛔
-
int Document::insertImage(int row, int column, const QImage &image) - 功能说明: 在当前活动工作表的row行、column列位置插入图像;
- 参数row: 插入图像的左上角位于row行(从1开始);
- 参数col: 插入图像的左上角位于col行(从1开始);
- 参数image: 需要插入的图片;
- 返回值:true:插入成功,false:插入失败;
-
uint Document::getImageCount() - 功能说明: 获取当前活动工作表中图片的个数;
- 返回值: 工作表中图片个数;
-
bool Document::getImage(int imageIndex, QImage& img) - 功能说明: 以【索引】方式获取当前活动工作表中索引为imageIndex的图片;
- **参数imageIndex:**需要获取的图片的索引(从1开始);
- 参数img: 用于保存获取的图片;
- 返回值: true:获取图片成功,false:获取图片失败;
-
bool Document::getImage(int row, int col, QImage &img) - 功能说明: 以【行列号】方式获取当前活动工作表中位于row行、col列的图片;
- 参数row: 图像的左上角位于row行(从1开始);
- 参数col: 图像的左上角位于col行(从1开始);
- 参数img: 用于保存获取的图片;
- 返回值: true:获取图片成功,false:获取图片失败;
#ifndef TEST5_H #define TEST5_H #include4.2 .cpp文件#include "Interface.h" namespace Ui { class Test5; } class Test5 : public InterFace { Q_OBJECT public: explicit Test5(QWidget *parent = nullptr); ~Test5(); QString getExcelName() override; private slots: void on_but_insert_clicked(); void on_but_getCount_clicked(); void on_but_getImage1_clicked(); void on_pushButton_4_clicked(); private: Ui::Test5 *ui; }; #endif // TEST5_H
#include "test5.h" #include "ui_test5.h" #include5、实现效果 6、源代码論#include #include QXLSX_USE_NAMESPACE #define EXCEL_NAME "image.xlsx" // 本Demo使用的Excel文件名 Test5::Test5(QWidget *parent) : InterFace(parent), ui(new Ui::Test5) { ui->setupUi(this); this->setWindowTitle("QXlsx在工作表中插入、读取图片"); this->setToolTip(this->windowTitle()); } Test5::~Test5() { delete ui; } QString Test5::getExcelName() { return EXCEL_NAME; } void Test5::on_but_insert_clicked() { Document xlsx; QImage image1("://image/C++.PNG"); QImage image2("://image/Qt.PNG"); qDebug() << "插入图片:"< qInfo() << "Excel保存成功!"; } else { qWarning() << "Excel保存失败!"; } } void Test5::on_but_getCount_clicked() { Document xlsx(EXCEL_NAME); if(!xlsx.load()) { QMessageBox::warning(this, "错误", QString("打开%1失败,可能是文件不存在!").arg(EXCEL_NAME)); return; } uint count = xlsx.getImageCount(); // 查询当前Sheet中图片数量 QMessageBox::about(this, "插入图片数", QString("共有%1张图片!").arg(count)); } void Test5::on_but_getImage1_clicked() { Document xlsx(EXCEL_NAME); if(!xlsx.load()) { QMessageBox::warning(this, "错误", QString("打开%1失败,可能是文件不存在!").arg(EXCEL_NAME)); return; } QImage image; bool ret = xlsx.getImage(1, image); // 读取当前Sheet中第1张图片(注意:索引是从1开始,而不是从0开始) if(ret) { ui->label->setPixmap(QPixmap::fromImage(image)); // 显示读取到的图片 } else { QMessageBox::warning(this, "错误", "读取图片失败,可能是不存在!"); } } void Test5::on_pushButton_4_clicked() { Document xlsx(EXCEL_NAME); if(!xlsx.load()) { QMessageBox::warning(this, "错误", QString("打开%1失败,可能是文件不存在!").arg(EXCEL_NAME)); return; } QImage image; bool ret = xlsx.getImage(23, 3, image); // 读取当前Sheet中第2张图片 if(ret) { ui->label->setPixmap(QPixmap::fromImage(image)); // 显示读取到的图片 } else { QMessageBox::warning(this, "错误", "读取图片失败,可能是不存在或位置错误!"); } }
gitee
github



