Qt使用html生成word文档
零、前言
项目中要用到把一些图片和一些报告保存为word文件,今天研究了一下,主要是使用html方法保存为word文档,这对html的使用有一定的要求,由于自己之前html的使用也不多所以搞了很久。下面在这分享一下自己的经验,希望对需要的有帮助。
一、效果图
二、核心代码
#include "widget.h"
#include "ui_widget.h"
#include <QFile>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
QString docname = "E:/testhtmlword.doc";
QString html = saveHtmlToWord();
QFile outFile(docname);
outFile.open(QIODevice::WriteOnly | QIODevice::Append );
QTextStream ts(&outFile);
ts<<html<<endl;
}
Widget::~Widget()
{
delete ui;
}
/// 组合html
QString Widget::saveHtmlToWord()
{
QString html = "";
html += "<html>";
html += "<head>";
html += "<title>qt实现生成word文档</title>";
html += "<head>";
html += "<body style=\"bgcolor:yellow\">";
html += "<h1 style=\"background-color:red\">测试qt实现生成word文档</h1>";
html += "<hr>";
html += "<p>word插入图片<img src=\"E:/logo.jpg\" alt=\"picture\" width=\"100\" height=\"100\"></p>";
html += "<hr>";
html += "<table width=\"100%\" border=\"1\" cellspacing=\"1\" cellpadding=\"4\" align=\"center\">";
html += "<tr>";
html += "<td align=\"center\" height=\"60\">编程语言统计</td>";
html += "</tr>";
html += "<tr>";
html += "<td align=\"center\" height=\"25\">2017-01-18---2018-01-18</td>";
html += "</tr>";
html += "</table>";
html += "<table width=\"100%\" border=\"1\" cellspacing=\"1\" cellpadding=\"4\" bgcolor=\"#cccccc\" align=\"center\">";
html += "<tr>";
html += "<th>C/C++</th>";
html += "<th>python</th>";
html += "<th>java</th>";
html += "<th>html</th>";
html += "<th>Qt</th>";
html += "</tr>";
html += "<tr>";
html += "<th>上升</th>";
html += "<th>上升</th>";
html += "<th>下降</th>";
html += "<th>下降</th>";
html += "<th>上升</th>";
html += "</tr>";
html += "</table>";
html += "</body>";
html += "</html>";
return html;
}
说明:Qt生成word文档还有其他方法,吾将上下而求索。
本文为博主原创文章,未经博主允许请勿转载!作者:ISmileLi