【QT】QtXlsx installation and use

Introduction to QtXlsx

QtXlsx is a library that can read and write Excel files. It does not require Microsoft Excel and can be used on any platform supported by Qt5. QT5 support must be required here.

  • Generate a new .xlsx file
  • Extract data from existing .xlsx file
  • Edit existing .xlsx file

The difference from QAxObject is that QAxObject requires the support of WPS or Office controls, and there are lags in reading and writing. If the PDF tool is installed on the computer, some abnormal errors may occur when calling it.

QtXlsx + Qt configuration

  1. Install perl

    Here you can choose to download and install from the official website (the installation method for lazy people is not listed here or you can download an old version from the Tencent application).
    Official website address: https://platform.activestate.com/tangxing806/ActivePerl-5.28/distributions

    To see whether the installation is successful, you can open the command prompt (CMD) to check, as shown below, the installation is OK:
    Insert image description here
    If it prompts that the command cannot be found, the installation was not successful!

  2. QtXlsx source code download

    Source code address: https://github.com/dbzhang800/qtxlsxwriter
    Insert image description here
    . After downloading, you will get the file as shown in the figure.
    Insert image description here
    Open Qt 5.14.2 (MinGW 7.3.0 32-bit) and follow the instructions below (select according to your own Qt version) ).

  3. MinGW32 configuration
    Enter the folder you just decompressed and find src as shown in the figure:
    Insert image description here
    switch to the current directory and
    Insert image description here
    execute the following instructions.

qmake src.pro
mingw32-make
mingw32-make install

Wait for the command to complete!

Then enter your own project .pro file and add
QT += xlsx.
The next step is to import the header file in the project
#include "xlsxdocument.h"
for reference. Simple example

Simple usage example

bool MainWindow::saveDataToExcel(const QString _strFilePath, QString _strFileName)
{
    
    
    // 检查路径下是否存在PGFile文件夹,存在则跳过,不存在就创建
    QString folderPath = _strFilePath + QString("/PGFile");
    qDebug()<< folderPath;

    // 创建QDir对象
    QDir dir;

    // 检查文件夹是否存在
    if(dir.exists(folderPath)) {
    
    
        qDebug()<< "文件夹已存在,跳过,请忽略错误";
    } else {
    
    
        // 尝试创建文件夹
        if(dir.mkpath(folderPath)) {
    
    
            // success
        } else {
    
    
            QMessageBox::information(this,QStringLiteral("错误提示"),QStringLiteral("PGFile文件夹创建失败"));
            return false;
        }
    }

    // 创建excel文件名
    QString excelName = folderPath + QString("/") + _strFileName;

#if 1   // 使用xlsx可以不依赖wps或者office,但是QAxObject依赖,并且很卡顿
    // 创建xlsx文件
    QXlsx::Document xlsx;

    RKQPoint writePoint;
    // 遍历需要写入的文件
    for(int i = 0; i < m_saveVec.size(); i++) {
    
    
        // 创建A行
        QString aString = QString("A%1").arg(i+1);
        xlsx.write(aString, m_saveVec[i].point_x);

        // 创建B行
        QString bString = QString("B%1").arg(i+1);
        xlsx.write(bString, m_saveVec[i].point_y);
    }

    // 写入excel文件
    xlsx.saveAs(excelName);
#else
    // 创建Excel应用对象
    QAxObject* excel = new QAxObject("Excel.Application");
    if(excel) {
    
    
        // 打开Excel文件
        QAxObject* workBooks = excel->querySubObject("workBooks");
        QAxObject* workBook = workBooks->querySubObject("Open(const QString&)",excelName);

        if(workBook) {
    
    
            // 获取第一个工作表
            QAxObject* workSheets = workBook->querySubObject("workSheets");
            QAxObject* workSheet = workSheets->querySubObject("Item(int)",1);

            if(workSheet) {
    
    
                // 将数据转换为QVariantList和QVariantMap
                QVariantList rows;
                for(int i = 0; i < m_saveVec.size(); ++i) {
    
    
                    QVariantMap row;
                    row["Gray"] = m_saveVec[i].point_x;
                    row["Power"] = m_saveVec[i].point_y;
                    rows.append(row);
                }

                // 写入数据到Excel表
                for(int n = 0; n < m_saveVec.size(); ++n) {
    
    
                    QAxObject* range = workSheet->querySubObject("Cells(int,int)",n+1, 1);
                    range->dynamicCall("SetValue(const QVariant&)",m_saveVec[n].point_x);
                    delete range;

                    range = workSheet->querySubObject("Cells(int,int)",n+1, 2);
                    range->dynamicCall("SetValue(const QVariant&)",m_saveVec[n].point_y);
                    delete range;
                }

                // 保存Excel文件
                workBook->dynamicCall("SaveAs(const QString&)",excelName);

                workSheet->dynamicCall("Activate()");
                workBook->dynamicCall("Close()");
                excel->dynamicCall("Quit()");

                delete workSheet;
                delete workSheets;
                delete workBook;
            }
        }

        delete workBooks;
        delete excel;
    }
#endif

    qDebug()<< "保存成功!";
    return true;
}

The project uses QAxObject and QtXlsx, record them.

Guess you like

Origin blog.csdn.net/m0_43458204/article/details/131602395