Embedded Qt development - Excel table data export

There is a demand for exporting embedded Excel table data: the application software runs on the embedded Linux platform, and a lot of data is stored during the operation of the device, and these data want to be exported in the form of a table. Considering the universality of the Windows platform, the data needs to be exported in the form of an excel form, so an open source library is selected: QtXlsxWriter, this article mainly records its compilation and construction process.

Obtain the installation package from the following address https://github.com/dbzhang800/QtXlsxWriter , unzip the source code package, and the directory structure is shown in the figure below:

  • Demos are stored in the example directory.

  • xlsxThe source code is stored in the src directory .

  • Test projects are stored in the tests directory.

QtXlsxWriterYou can compile and build through QtCreator, so we open QtCreator installed on the linux platform, and then use QtCreator to open the QtXlsxWriter project (select the xxx.pro file in the source code directory). After selecting the corresponding compilation build kit, as shown in the following figure:

Click the top-level directory in the project management view, right-click, and select "Build" to build QtXlsxWriter:

After the compilation is successful, a build output directory will be created at the same level as the source code directory. The directory structure is shown in the following figure:

  • bin : used to store binary files. (the directory is empty)
  • examples: The compilation and build output directory of the example Demo.
  • include: This directory stores header files about QtXlsxWriter:

  • lib: This directory is used to store the library directory of QtXlsxWriter:

  • mkspecs: This directory is the modular identification directory of Qt, which is used to add Xlsx to the Qt development environment:

  • tests: This directory is used to store test projects.

Among the above directories, include, lib, and mkspecsdirectories are more important directories. It needs to be used when building the QtXlsxWriter development environment.

Copy the bin, include, lib, mkspecs, and src directories in the built directory to the Qt installation directory, so that the Qt+=xlsxloading module can be used in QtCreator

Then you can compile and build a demo example to test it. This article takes chartas an example, the code is as follows:

#include <QtCore>
#include "xlsxdocument.h"
#include "xlsxcellrange.h"
#include "xlsxchart.h"

using namespace QXlsx;

int main()
{
    
    
    //![0] 创建xlsx文档对象。向单元格总写入数据
    Document xlsx;
    for (int i = 1; i < 10; ++i) {
    
    
        xlsx.write(i, 1, i * i * i); // A1:A9
        xlsx.write(i, 2, i * i); // B1:B9
        xlsx.write(i, 3, i * i - 1); // C1:C9
    }
    //![0]

    //![1]创建图表
    Chart *pieChart = xlsx.insertChart(3, 3, QSize(300, 300));
    pieChart->setChartType(Chart::CT_Pie);
    pieChart->addSeries(CellRange("A1:A9"));
    pieChart->addSeries(CellRange("B1:B9"));
    pieChart->addSeries(CellRange("C1:C9"));

    Chart *pie3DChart = xlsx.insertChart(3, 9, QSize(300, 300));
    pie3DChart->setChartType(Chart::CT_Pie3D);
    pie3DChart->addSeries(CellRange("A1:C9"));

    Chart *barChart = xlsx.insertChart(23, 3, QSize(300, 300));
    barChart->setChartType(Chart::CT_Bar);
    barChart->addSeries(CellRange("A1:C9"));

    Chart *bar3DChart = xlsx.insertChart(23, 9, QSize(300, 300));
    bar3DChart->setChartType(Chart::CT_Bar3D);
    bar3DChart->addSeries(CellRange("A1:C9"));

    Chart *lineChart = xlsx.insertChart(43, 3, QSize(300, 300));
    lineChart->setChartType(Chart::CT_Line);
    lineChart->addSeries(CellRange("A1:C9"));

    Chart *line3DChart = xlsx.insertChart(43, 9, QSize(300, 300));
    line3DChart->setChartType(Chart::CT_Line3D);
    line3DChart->addSeries(CellRange("A1:C9"));

    Chart *areaChart = xlsx.insertChart(63, 3, QSize(300, 300));
    areaChart->setChartType(Chart::CT_Area);
    areaChart->addSeries(CellRange("A1:C9"));

    Chart *area3DChart = xlsx.insertChart(63, 9, QSize(300, 300));
    area3DChart->setChartType(Chart::CT_Area3D);
    area3DChart->addSeries(CellRange("A1:C9"));

    Chart *scatterChart = xlsx.insertChart(83, 3, QSize(300, 300));
    scatterChart->setChartType(Chart::CT_Scatter);
    // Will generate three lines.
    scatterChart->addSeries(CellRange("A1:A9"));
    scatterChart->addSeries(CellRange("B1:B9"));
    scatterChart->addSeries(CellRange("C1:C9"));

    Chart *scatterChart_2 = xlsx.insertChart(83, 9, QSize(300, 300));
    scatterChart_2->setChartType(Chart::CT_Scatter);
    // Will generate two lines.
    scatterChart_2->addSeries(CellRange("A1:C9"));

    Chart *doughnutChart = xlsx.insertChart(103, 3, QSize(300, 300));
    doughnutChart->setChartType(Chart::CT_Doughnut);
    doughnutChart->addSeries(CellRange("A1:C9"));
    //![1]

    //![2]将xlsx文档对象保存为Book1.xlsx文件。
    xlsx.saveAs("Book1.xlsx");
    //![2]
    
    //保存为Book2.xlsx文件。
    Document xlsx2("Book1.xlsx");
    xlsx2.saveAs("Book2.xlsx");
    return 0;
}

Select the example directory in the project management view, expand it in turn, select chartthe directory, right-click, and select the "Build" option to build the chart demo.

Then in the compilation and construction directory of the chart, the following files will be generated:

Run the generated program in the terminal (here chart):

After the operation is complete, two files will be created in the same directory: as shown in the following figure:

Note: Since it is compiled and built on ubuntu, there is no application software that can open xlsx, so copy it to Windows and use wps to open it. After opening, the chart is as shown in the figure below:

The above process is carried out under Ubuntu18.04. If you want QtXlsxWriter to run on the embedded linux platform, you only need to select the corresponding cross-compilation kit when selecting the compilation build kit in QtCreator. After the compilation is completed, the built library will be Copy the file to the link library directory of Linux (usually /usr/lib), then you can use QtXlsxWriter on the embedded Linux platform.

Guess you like

Origin blog.csdn.net/iriczhao/article/details/127233603