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:
- Locate the
QXlsxfolder within the downloaded source. - Copy the contents of this folder into your project root directory.
- Modify your project's
.profile 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.
- Ensure the
README.mdfile from the QXlsx source is present in theQXlsxfolder, as the build script may reference it. - 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 includeusing namespace QXlsx;. - Dynamic Casting: When retrieving sheets via
excel.workbook()->sheet(index), the returned object is a base class pointer. Usedynamic_cast<QXlsx::Worksheet*>to access specific spreadsheet functions. - Build Errors: If using CMake and encountering missing file errors, verify that the
QXlsxfolder contains all necessary source files and theCMakeLists.txtprovided by the library author.