一、下载地址:
二、进入下载好的QXlsx目录下,取出QXlsx目录和README.md待用
三、用qt创建一个简单的cmake工程,将QXlsx目录和README.md文件放到cmakelists.txt所在目录
四、修改cmakelists.txt文件
cmake_minimum_required(VERSION 3.5) project(xlsxTest LANGUAGES CXX) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets Core Gui REQUIRED) find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Widgets Gui REQUIRED) add_subdirectory(QXlsx) #添加 if(ANDROID) add_library(xlsxTest SHARED main.cpp mainwindow.cpp mainwindow.h mainwindow.ui ) else() add_executable(xlsxTest main.cpp mainwindow.cpp mainwindow.h mainwindow.ui ) endif() target_link_libraries(xlsxTest PRIVATE Qt5::Widgets QXlsx::QXlsx) #增加 QXlsx::QXlsx
五、在QtCreator查看工程结构
六、代码
#include "mainwindow.h" #include "./ui_mainwindow.h" #include <QCoreApplication> #include <QDebug> #include <QDir> #include <QVariant> #include <QtCore> #include <QtGlobal> #include "xlsxcellrange.h" #include "xlsxchart.h" #include "xlsxchartsheet.h" #include "xlsxdocument.h" #include "xlsxrichstring.h" #include "xlsxworkbook.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); // 第一步:定义xlsw QXlsx::Document xlsxR("Test.xlsx"); // 第二布:加载 if (xlsxR.load()) // load excel file { xlsxR.selectSheet(xlsxR.sheetNames()[0]); QXlsx::CellRange range = xlsxR.dimension(); // 第三步:遍历,获取数据 for(int i =1;i<=range.columnCount();i++) { for(int j =1;j<=range.rowCount();++j) { if(xlsxR.cellAt(j,i)) { // 遍历行和列,拿到数据 :xlsxR.cellAt(i,j)->value().toInt() qDebug()<< xlsxR.cellAt(j,i)->value().toByteArray(); } } } } else { qDebug() << "打开文件失败"; } } MainWindow::~MainWindow() { delete ui; }
输出:
七、text.xlsx文件自备
标签:execl,QXlsx,set,CMAKE,源码,xlsxR,include,mainwindow From: https://www.cnblogs.com/RedWetPlace/p/17777925.html