1. 程式人生 > >基於 QtXlsxWriter 的excel表格生成

基於 QtXlsxWriter 的excel表格生成

 QtXlsxWriter 第三方庫 雖然幾年沒有更新,但是目前來看 就操作 excel表格而言,仍然十分方便。

二,編譯

  生成庫檔案 和標頭檔案引用


三,應用

(1),工程 包含   

QT+=coreguixlsx

(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(newUi::Widget)
{
ui->setupUi(this);
QStringaircrew;//機組
QStringflightDate;//日期
QStringflightPlace;//起飛地點
QStringsorties;//架次
QXlsx::Documentxlsx;
Formatformat1;
format1.setFontBold(true);
format1.setBorderStyle(Format::BorderThin);
format1.setPatternBackgroundColor(Qt
::lightGray);
//外邊框灰色
Formatf_border;
f_border.setPatternBackgroundColor(Qt::lightGray);
f_border.setBorderStyle(Format::BorderThin);
for(introw=2;row<=8;++row){
for(intcol=1;col<=9;++col)
xlsx.write(row,col,QVariant(),f_border);
}
FormatcenterAlign;
centerAlign.setHorizontalAlignment(Format::AlignHCenter
);
centerAlign.setVerticalAlignment(Format::AlignVCenter);
centerAlign.setBorderStyle(Format::BorderThin);//外邊框
centerAlign.setPatternBackgroundColor(Qt::yellow);
centerAlign.setFontBold(true);
FormatcenterTitle;
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);
boolisSave=xlsx.saveAs("D:\\FD\\fgroundcontrol\\qgroundcontrol\\libs\\QtXlsxWriter-master\\build\\myTest\\Test1.xlsx");
qDebug()<<"isSave"<<isSave;
}
Widget::~Widget()
{
deleteui;
}