Qt下Excel报表生成的又一利器----QtXlsxWriter

来源:互联网 发布:淘宝好评评语怎么删除 编辑:程序博客网 时间:2024/06/06 01:49

项目主页:

http://qtxlsx.debao.me/

Git地址:

https://github.com/dbzhang800/QtXlsxWriter


      最近要做QT下的报表生成,本来想自己手写HTML生成再做一个转换,动手前搜了一下相关的库,还真有不少,

LIBXL,basicExcel等等。但若基于QT开发,QtXlsxWriter与之衔接十分方便,于是便下载下来试试,确实好用。


首先 确保安装了QT

配置QMAKE环境变量


export PATH="/qt installed dir/bin":$PATH

git clone https://github.com/dbzhang800/QtXlsxWriter

qmake

(sudo) make install                                                  

sudo 视QT安装目录权限决定




QT中的.pro加入第二行语句

QT       += core gui
QT       += xlsx

头文件加入

#include "xlsxdocument.h"

命名空间为:QXlsx


试试以下的代码

    QXlsx::Document xlsx;    Format format;    format.setHorizontalAlignment(Format::AlignHCenter);    format.setVerticalAlignment(Format::AlignVCenter);    xlsx.write("A1", "Caption");    xlsx.mergeCells("A1:H1", format);    //Set the height of the first row to 50.0(points)    xlsx.setRowHeight(1, 50.0);    //Set the width of the third column to 40.0(chars)    //xlsx.setColumnWidth(3, 3, 40.0);    //Set style for the row 11th.    QXlsx::Format format1;    format1.setFontBold(true);    format1.setFontColor(QColor(Qt::blue));    format1.setFontSize(20);    xlsx.write(11, 1, "Hello Row Style");    xlsx.write(11, 6, "Blue Color");    xlsx.setRowFormat(11, format1);    xlsx.setRowHeight(11, 41);    //Set style for the col [9th, 16th)    QXlsx::Format format2;    format2.setFontBold(true);    format2.setFontColor(QColor(Qt::magenta));    for (int row=2; row<=16; row++)        for (int col=1; col<=8; col++)            xlsx.write(row, col, row+col);    xlsx.setColumnWidth(9, 16, 5.0);    xlsx.setColumnFormat(9, 16, format2);    xlsx.save();




就可以看到如下的效果了:



0 0
原创粉丝点击