基于 QtXlsxWriter 的excel表格生成

 QtXlsxWriter 第三方库 虽然几年没有更新,但是目前来看 就操作 excel表格而言,仍然十分方便。

一,git 地址 :https://github.com/dbzhang800/QtXlsxWriter

       项目主页:http://qtxlsx.debao.me/

二,编译

  生成库文件 和头文件引用

基于 QtXlsxWriter 的excel表格生成

三,应用

(1),工程 包含   

QT += core gui xlsx


(2),示例 代码

xlsx.write(地址,格式)

#include "widget.h"
#include "ui_widget.h"

#include <QDebug>
#include "xlsxdocument.h"
#include "xlsxrichstring.h"
#include "xlsxworkbook.h"
#include "xlsxformat.h"
#include "xlsxconditionalformatting.h"
QTXLSX_USE_NAMESPACE

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    QString aircrew;//机组
    QString flightDate;//日期
    QString flightPlace;//起飞地点
    QString sorties;//架次

    QXlsx::Document xlsx;

    Format format1;
    format1.setFontBold(true);
    format1.setBorderStyle(Format::BorderThin);
    format1.setPatternBackgroundColor(Qt::lightGray);

    //外边框 灰色
    Format f_border;
    f_border.setPatternBackgroundColor(Qt::lightGray);
    f_border.setBorderStyle(Format::BorderThin);
    for (int row=2; row<=8; ++row) {
        for (int col=1; col<=9; ++col)
            xlsx.write(row, col,QVariant(),f_border);
    }

    Format centerAlign;
    centerAlign.setHorizontalAlignment(Format::AlignHCenter);
    centerAlign.setVerticalAlignment(Format::AlignVCenter);
    centerAlign.setBorderStyle(Format::BorderThin);//外边框
    centerAlign.setPatternBackgroundColor(Qt::yellow);
    centerAlign.setFontBold(true);

    Format centerTitle;
    centerTitle.setHorizontalAlignment(Format::AlignHCenter);
    centerTitle.setVerticalAlignment(Format::AlignVCenter);
    xlsx.write("A1", QStringLiteral("航摄飞行记录表"));
    xlsx.mergeCells("A1:I1",centerTitle);

    xlsx.write("A2",QStringLiteral("起飞"),centerAlign);
    xlsx.write("B2",QStringLiteral("机组"),format1);
    xlsx.write("C2",aircrew,f_border);
    xlsx.write("D2",QStringLiteral("日期"),format1);
    xlsx.write("E2",flightDate,f_border);
    xlsx.write("F2",QStringLiteral("起飞地点"),format1);
    xlsx.write("G2",flightPlace,f_border);
    xlsx.write("H2",QStringLiteral("架次"),format1);
    xlsx.write("I2",sorties,f_border);

    xlsx.write("A3",QStringLiteral("摄区"),centerAlign);
    xlsx.write("B3",QStringLiteral("相对高度"),format1);
    xlsx.write("D3",QStringLiteral("航区角度"),format1);
    xlsx.write("F3",QStringLiteral("航线条数"),format1);
    xlsx.write("H3",QStringLiteral("地面分辨率"),format1);
    xlsx.mergeCells("A3:A4",centerAlign);

    xlsx.write("B4",QStringLiteral("航向重叠度"),format1);
    xlsx.write("D4",QStringLiteral("旁向重叠度"),format1);
    xlsx.write("F4",QStringLiteral("航线间隔"),format1);
    xlsx.write("H4",QStringLiteral("基线长度"),format1);

    xlsx.write("A5",QStringLiteral("飞机"),centerAlign);
    xlsx.write("B5",QStringLiteral("飞机型号"),format1);
    xlsx.write("D5",QStringLiteral("飞机编号"),format1);
    xlsx.write("F5",QStringLiteral("飞控型号"),format1);
    xlsx.write("H5",QStringLiteral("飞行速度"),format1);

    xlsx.write("A6",QStringLiteral("航摄仪"),centerAlign);
    xlsx.write("B6",QStringLiteral("航摄仪型号"),format1);
    xlsx.write("D6",QStringLiteral("焦距"),format1);
    xlsx.write("F6",QStringLiteral("拍照模式"),format1);
    xlsx.write("H6",QStringLiteral("POS"),format1);


    xlsx.write("A7",QStringLiteral("机组"),centerAlign);
    xlsx.write("B7",QStringLiteral("操控手"),format1);
    xlsx.write("D7",QStringLiteral("地面站"),format1);
    xlsx.write("F7",QStringLiteral("测量电压"),format1);
    xlsx.write("H7",QStringLiteral("落地电压"),format1);

    xlsx.write("A8",QStringLiteral("降落"),centerAlign);
    xlsx.write("B8",QStringLiteral("起飞时间"),format1);
    xlsx.write("D8",QStringLiteral("降落时间"),format1);
    xlsx.write("F8",QStringLiteral("RTK"),format1);
    xlsx.write("H8",QStringLiteral("地面站电压"),format1);

    bool isSave =  xlsx.saveAs("D:\\FD\\fgroundcontrol\\qgroundcontrol\\libs\\QtXlsxWriter-master\\build\\myTest\\Test1.xlsx");
    qDebug()<<"isSave"<<isSave;
}

Widget::~Widget()
{
    delete ui;
}

基于 QtXlsxWriter 的excel表格生成