Integrating the QXlsx Library for Excel Operations in Qt6 Projects

For Excel data manipulation in Qt, while QAxObject is a common choice, its performance is often sluggish for large dataests. The QXlsx library provides a more efficient, cross-platform altrenative compatible with both Qt5 and Qt6. This guide demonstrates how to integrate the QXlsx source into your project environment using both qmake and CMake build systems on Windows.

Repository Acquisition

The most active version of the library is maintained in the QtExcel/QXlsx repository. Download the source code as a ZIP archive and extract it to access the core library files and configuration scripts.

Configuration via qmake (.pro)

To integrate QXlsx directly into a qmake project, follow these steps:

  1. Locate the QXlsx folder within the downloaded source.
  2. Copy the contents of this folder into your project root directory.
  3. Modify your project's .pro file to include the library's internal configuration file.
# Define paths relative to the project root
QXLSX_PARENTPATH = ./
QXLSX_HEADERPATH = ./header/
QXLSX_SOURCEPATH = ./source/

# Include the library's PRI file
include(./QXlsx.pri)

Configuration via CMake (CMakeLists.txt)

For CMake-based projects, the library is best integrated as a subdirectory. Ensure the entire QXlsx source folder is copied into your project directory.

  1. Ensure the README.md file from the QXlsx source is present in the QXlsx folder, as the build script may reference it.
  2. Add the following commands to your CMakeLists.txt:
# Include the QXlsx subdirectory
add_subdirectory(QXlsx)

# Link the library to your target application
target_link_libraries(MyProjectName PRIVATE QXlsx::QXlsx)

Note: Place add_subdirectory before the target_link_libraries command, typically after the find_package calls.

Writing Excel Files

The following snippet demonstrates creating a basic spreadsheet. Note that the QXlsx namespace must be used.

#include "xlsxdocument.h"
#include "xlsxworkbook.h"

void generateReport() {
    QXlsx::Document doc;
    
    // Cell addressing uses (Row, Column) or string references
    doc.write("A1", "Generated by Qt6");
    doc.write(2, 1, 100.50);
    
    if (doc.saveAs("Output.xlsx")) {
        // File saved successfully
    }
}

Reading Excel Data into QTableWidget

When reading data, account for the coordinate system difference: Excel uses 1-based indexing (1, 1), while QTableWidget uses 0-based indexing (0, 0).

#include <QFileDialog>
#include <QTableWidget>
#include "xlsxdocument.h"
#include "xlsxworksheet.h"

void importToTable(QTableWidget *uiTable) {
    QString path = QFileDialog::getOpenFileName(nullptr, "Open Excel", "", "*.xlsx");
    if (path.isEmpty()) return;

    QXlsx::Document excel(path);
    if (excel.load()) {
        // Access the first worksheet
        auto *sheet = dynamic_cast<QXlsx::Worksheet*>(excel.workbook()->sheet(0));
        if (!sheet) return;

        const int maxRows = 10;
        const int maxCols = 5;
        
        uiTable->setRowCount(maxRows);
        uiTable->setColumnCount(maxCols);

        for (int r = 1; r <= maxRows; ++r) {
            for (int c = 1; c <= maxCols; ++c) {
                QVariant data = sheet->read(r, c);
                if (data.isValid()) {
                    uiTable->setItem(r - 1, c - 1, new QTableWidgetItem(data.toString()));
                } else {
                    uiTable->setItem(r - 1, c - 1, new QTableWidgetItem(""));
                }
            }
        }
    }
}

Implementation Notes

  • Namespace: Always prefix library classes with QXlsx:: or include using namespace QXlsx;.
  • Dynamic Casting: When retrieving sheets via excel.workbook()->sheet(index), the returned object is a base class pointer. Use dynamic_cast<QXlsx::Worksheet*> to access specific spreadsheet functions.
  • Build Errors: If using CMake and encountering missing file errors, verify that the QXlsx folder contains all necessary source files and the CMakeLists.txt provided by the library author.

Tags: Qt6 QXlsx Excel CMake qmake

Posted on Thu, 25 Jun 2026 17:18:56 +0000 by SJR_34