QtExcel/QXlsx

GitHub地址:

https://github.com/QtExcel/QXlsx

1.3k的star,也挺多的。

使用Qt5 或 6的Excel 文件(*.xlsx)读取/写入库。

QtXlsxWriter的派生物

Descendant : 派生物


QtExcel的一个成员:

热衷于编写C++和Qt软件,已经超过10年了。

所属国家​​:巴西(Brazil)

行政地位​​:圣保罗州首府,巴西最大城市


首尔,南韩


QXlsx | Excel file(*.xlsx) reader/writer library using Qt 5 or 6. Descendant of QtXlsxWriter.

有简体中文版的介绍:

QXlsx | Excel file(*.xlsx) reader/writer library using Qt 5 or 6. Descendant of QtXlsxWriter.

很棒很棒!


关键点:

Qt框架,C++,Excel的读取/写入

这里提到:QtXlsx停止维护了。

Qt Xlsx-CSDN博客

确实,最近一次提交是在2020年了。

使用方法:

(1)把源码塞到项目中

(2)把它作为第三方库来使用。


如何安装QXlsx project?

如何设置 QXlsx 项目 | QXlsx

有中文版本,nice

我直接把zip下载下来啦,因为我的git好像配置的有点问题

md文件还挺多的,可以用vnote来查看。

选择'New File or Project'

选择'Qt Console Application',也可以选择'Qt Widgets Application'

(人家的文档写的真用心啊,手把手教)

拷贝文件到自己的项目的目录

在.pro文件中添入这段代码:

复制代码
# QXlsx code for Application Qt project
QXLSX_PARENTPATH=./         # current QXlsx path is . (. means curret directory)
QXLSX_HEADERPATH=./header/  # current QXlsx header path is ./header/
QXLSX_SOURCEPATH=./source/  # current QXlsx source path is ./source/
include(./QXlsx.pri)

这种写法,是直接把代码嵌入到项目中了,可以看到,生成了很多相关的.o文件。

测试一下给的这个示例代码:

cpp 复制代码
// main.cpp

#include <QCoreApplication>

#include "xlsxdocument.h"
#include "xlsxchartsheet.h"
#include "xlsxcellrange.h"
#include "xlsxchart.h"
#include "xlsxrichstring.h"
#include "xlsxworkbook.h"
using namespace QXlsx;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QXlsx::Document xlsx;
    xlsx.write("A1", "Hello Qt!"); // write "Hello Qt!" to cell(A,1). it's shared string.
    xlsx.saveAs("Test.xlsx"); // save the document as 'Test.xlsx'

    return 0;
    // return a.exec();
}

可以看到,这个库,使用起来还是挺方便的。


示例:

QXlsx Examples | QXlsx

示例:

写入excel,然后读取excel中的内容。

cpp 复制代码
// main.cpp

#include <QtGlobal>
#include <QCoreApplication>
#include <QtCore>
#include <QVariant>
#include <QDebug>

//#include <iostream>
//using namespace std;

// [0] include QXlsx headers
#include "xlsxdocument.h"
#include "xlsxchartsheet.h"
#include "xlsxcellrange.h"
#include "xlsxchart.h"
#include "xlsxrichstring.h"
#include "xlsxworkbook.h"
using namespace QXlsx;

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    int row = 1; int col = 1;

    // [1]  Writing excel file(*.xlsx)
    QXlsx::Document xlsxW;
    QVariant writeValue = QString("Hello Qt!");
    xlsxW.write(row, col, writeValue); // write "Hello Qt!" to cell(A,1).
    xlsxW.saveAs("Test.xlsx"); // save the document as 'Test.xlsx'

    // [2] Reading excel file(*.xlsx)
    Document xlsxR("Test.xlsx");
    if (xlsxR.load()) // load excel file
    {
        auto cell = xlsxR.cellAt(row, col); // get cell pointer.
        if ( cell != NULL )
        {
            QVariant var = cell->readValue(); // read cell value (number(double), QDateTime, QString ...)
            qDebug() << var; // display value. it is 'Hello Qt!'.
        }
    }

    return 0;
}