The article directories

      • Qt report QtXlsx
      • Using code examples
        • Source reference
        • The sample code
      • The effect of generating a table
      • conclusion

Qt report QtXlsx

QtXlsx is an open source Excel file reading and writing tool, it is relatively simple to use, and does not rely on Windows Office software, can directly generate Excel files;

Using code examples

Source reference

In the project file (.pro) file, directly reference the QtXlsx project file:

include(QtXlsxWriter/src/xlsx/qtxlsx.pri)
Copy the code

The sample code

The comments in the code are more detailed than they are written, so I will not specify the individual functions here

void testQtxlsx(a)
{
    QXlsx::Document xlsx;

    xlsx.setColumnWidth(1.7.12);  // Set the column width

    QXlsx::Format centerAlign;
    centerAlign.setHorizontalAlignment(QXlsx::Format::AlignHCenter); // Set horizontal and vertical centers
    centerAlign.setVerticalAlignment(QXlsx::Format::AlignVCenter);   //

    centerAlign.setFontBold(true);          // Make the font bold
    centerAlign.setFontColor(Qt::blue);     // Set the font color
    centerAlign.setFontSize(30);            // Set the font size
    // Set the top, bottom, left, and right lines
    centerAlign.setTopBorderStyle(QXlsx::Format::BorderThin);
    centerAlign.setBottomBorderStyle(QXlsx::Format::BorderThin);
    centerAlign.setLeftBorderStyle(QXlsx::Format::BorderThin);
    centerAlign.setRightBorderStyle(QXlsx::Format::BorderThin);

    xlsx.mergeCells(1.1.1.7,centerAlign); // Merge cell tables
    xlsx.write("A1"."Test Results Report");       / / title


    centerAlign.setFontColor(Qt::black);  // Set the font color
    centerAlign.setFontSize(13);          // Set the font size
    xlsx.mergeCells(2.1.1.7,centerAlign); // Subtitle merge cell table, 2- second row, 1-7 column merge
    xlsx.write("A2"."Lab Report No. 1");        / / subtitle

    xlsx.mergeCells(3.1.1.2,centerAlign);   // Merge cell tables
    xlsx.write("A3"."Tester:",centerAlign);  //
    xlsx.mergeCells(3.3.1.2,centerAlign);
    xlsx.write("C3"."Result: Qualified");

    xlsx.mergeCells(3.5.1.3,centerAlign);
    xlsx.write("E3"."Detection time: 2020-12-13",centerAlign);

    centerAlign.setFontBold(false);
    centerAlign.setFontSize(10);


    // Populate the test data unit table
    for (int row = 4; row < 50; row++)
    {
        for (int col = 0; col < 7; col++ )
        {
            xlsx.write(row,col+1, col,centerAlign); }}// save files directly to the program directory
    QString strPath = QGuiApplication::applicationDirPath() +"/"+"test"+".xlsx";
    xlsx.saveAs(strPath);
    
	return;
}
Copy the code

The effect of generating a table



Print preview:

conclusion

If the data is not too special, it is easy to use this method. In addition, the use of this tool can be achieved icon statistics, have time to study their own research.

Wechat Official Account: