Qt 生成 Word、PPT、PDF 文档

本文档介绍了如何使用Qt库来生成Word、PowerPoint(PPT)和PDF文件。详细讲解了从HTML格式转换、利用QAxObject插入图片以及通过模板或直接插入图片的方式创建PPT。同时,还涵盖了Word转PDF、PPT转PDF的方法,以及Qt中使用QPainter和QPdfWriter生成PDF的限制和注意事项。
摘要由CSDN通过智能技术生成

一、帮助手册

微软的官方文档链接 https://msdn.microsoft.com/zh-cn/vba/word-vba/articles/documents-open-method-word

// 代码打印官方帮助文档
// 包含头文件 #include <ActiveQt/QAxObject>
// 包含lib Qt5AxContainerd.lib、Qt5AxServerd.lib、Qt5AxBased.lib

// 获取 Word 帮助文档;
QAxObject *pWordApplication = new QAxObject("Word.Application", 0);
QString docWord = pWordApplication->generateDocumentation();

QFile outFile("E:/wordLog.html");
outFile.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream ts(&outFile);
ts << docWord << endl;

// 获取 PPT 帮助文档;
QAxObject *pPPTApplication = new QAxObject("PowerPoint.Application", 0);
QString docPPT = pPPTApplication ->generateDocumentation();

QFile outFile("E:/wordLog.html");
outFile.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream ts(&outFile);
ts << docWord << endl;

二、生成 Word 文档

1. 使用 HTML 格式输出

    限制:不支持 .docx 格式的 Word 文档

void LHCCreateOfficeAPDF::UseHTMLToWord()
{
    QString docname = "E:/testhtmlword.doc";
    QString html = GetHtmlInfoToFile();
    QFile outFile(docname);
    outFile.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream ts(&outFile);
    ts << html << endl;
}
QString LHCCreateOfficeAPDF::GetHtmlInfoToFile()
{
    QString imageData = "\"E:/1.png\"";

    QDateTime current_date_time = QDateTime::currentDateTime();
    QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss ddd");
    
    // 有中文时,必须加,否则 html 转成 word 时 中文会乱码;
    QString html = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
    html += "<html>";
    html += "<head>";
    html += QStringLiteral("<title>qt实现生成word文档</title>");
    html += "<head>";
    html += "<body style=\"bgcolor:yellow\">";
    html += "<h2 align=\"center\">model</h2>";
    html += "<h4 align=\"center\">" + current_date + "</h2><br>";
    html += "<h1 style=\"background-color:red\">测试qt实现生成word文档</h1>";
    html += "<hr>";
    html += "<p>word插入图片<img src=" + imageData + " 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 += "
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值