一、帮助手册
微软的官方文档链接 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 += "