很高兴在雪易的CSDN遇见你 ,给你糖糖
欢迎大家加入雪易社区-CSDN社区云
前言
本文分享基于QT进行Word模板编辑以及Word转PDF的技术,希望对各位小伙伴有所帮助!
感谢各位小伙伴的点赞+关注,小易会继续努力分享,一起进步!
你的点赞就是我的动力(^U^)ノ~YO
目录
前言
1. 制作Word模板
2. 基于QT编辑Word模板
3. Word转PDF
结论:
1. 制作Word模板-添加书签
》新建Word文档
》插入---标签
将待插入内容制作为标签
》保存为.dot格式
2. 基于QT编辑Word模板
》书签 替换为 文本
//新建一个word应用程序
QAxWidget* word = new QAxWidget("Word.Application", 0, Qt::MSWindowsOwnDC);
//并设置为不可见
word->setProperty("Visible", false);
//获取所有的工作文档
QAxObject* documents = word->querySubObject("Documents");
//以test2.dot为模板新建一个文档
documents->dynamicCall("Add(QString)", strTemplatePath);
//获取当前激活的文档
QAxObject* document = word->querySubObject("ActiveDocument");
//获取文档中名字为name的标签
QAxObject* bookmark_name = document->querySubObject("Bookmarks(QVariant)", "name");
//选中标签,将字符textg插入到标签位置
if (!bookmark_name->isNull())
{
QString sText = ui->lineEdit_name->text(); //此处为替换内容
bookmark_name->dynamicCall("Select(void)"); //选中要选中的区域
bookmark_name->querySubObject("Range")->setProperty("Text", sText); //进行替换操作
}
//获取文档中名字为sex的标签
QAxObject* bookmark_sex = document->querySubObject("Bookmarks(QVariant)", "sex");
//选中标签,将字符textg插入到标签位置
if (!bookmark_sex->isNull())
{
QString sText = ui->lineEdit_sex->text(); //此处为替换内容
bookmark_sex->dynamicCall("Select(void)"); //选中要选中的区域
bookmark_sex->querySubObject("Range")->setProperty("Text", sText); //进行替换操作
}
//将文件保存为doc,同样可以生成docx文档
QString pathsave = QApplication::applicationDirPath() + QString::fromLocal8Bit("\\Report\\report_template.docx");
if (pathsave.isEmpty() == true)
{
return;
}
document->dynamicCall("SaveAs(const QString&))", QDir::toNativeSeparators(pathsave));
document->dynamicCall("Close (boolean)", false);
word->dynamicCall("Quit()");
QMessageBox::warning(this, "完成", "文件已经保存", QMessageBox::Yes);
》书签 替换为 图片
bool insertPic(QAxObject* document, QString sLabel, QString sFile)
{
if (!document)
return false;
QAxObject* bookmark_pic = document->querySubObject("Bookmarks(QString)", sLabel);
if (bookmark_pic)
{
bookmark_pic->dynamicCall("Select(void)");
QAxObject* range = bookmark_pic->querySubObject("Range");
QVariant tmp = range->asVariant();
QList<QVariant> qList;
qList << QVariant(sFile);
qList << QVariant(false);
qList << QVariant(true);
qList << tmp;
QAxObject* Inlineshapes = document->querySubObject("InlineShapes");
Inlineshapes->dynamicCall("AddPicture(const QString&,QVariant,QVariant,QVariant)", qList);
delete Inlineshapes;
}
return true;
}
3. Word转PDF
void zxKneeReportWidget::convertToPDF(QString strWordPath, QString strTargetPath)
{
if (strWordPath.isEmpty() || strTargetPath.isEmpty()) return;
QAxObject* pWordApplication = new QAxObject("Word.Application", 0);
QAxObject* pWordDocuments = pWordApplication->querySubObject("Documents");
QString fileName = strWordPath;
QString toFilePath = strTargetPath;
QVariant filename(fileName);
QVariant confirmconversions(false);
QVariant readonly(true);
QVariant addtorecentfiles(false);
QVariant passworddocument("");
QVariant passwordtemplate("");
QVariant revert(false);
//打开
QAxObject* doc = pWordDocuments->querySubObject("Open(const QVariant&, const QVariant&,const QVariant&, "
"const QVariant&, const QVariant&, "
"const QVariant&,const QVariant&)",
filename,
confirmconversions,
readonly,
addtorecentfiles,
passworddocument,
passwordtemplate,
revert);
QVariant OutputFileName(toFilePath);
QVariant ExportFormat(17); //17是pdf
QVariant OpenAfterExport(false); //保存后是否自动打开
//转成pdf
doc->querySubObject("ExportAsFixedFormat(const QVariant&,const QVariant&,const QVariant&)",
OutputFileName,
ExportFormat,
OpenAfterExport
);
//关闭
doc->dynamicCall("Close(boolean)