#include <QCoreApplication>
#include <QApplication>
#include <QDir>
#include <QFileDialog>
#include <QAxObject>
#include <QAxWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//新建一个word应用程序
QAxWidget *word = new QAxWidget("Word.Application");
if(word == NULL)
{
return 0;
}
//并设置为不可见
word->setProperty("Visible", false);
//获取所有的工作文档
QAxObject *documents = word->querySubObject("Documents");
if (documents == NULL)
{
return 0;
}
//以template.dotx为模板新建一个文档
documents->dynamicCall("Add(QString)", QString("D:/template.dotx"));
//获取当前激活的文档
QAxObject *document = word->querySubObject("ActiveDocument");
if (document == NULL)
{
return 0;
}
//获取文档中名字为Customer_Name的标签
QAxObject *pCustomer_Name = document->querySubObject("Bookmarks(QVariant)", QString("Customer_Name"));
//选中标签,将字符sText插入到标签位置
if (!pCustomer_Name->isNull())
{
QString sText = "CJX";//此处为替换内容
pCustomer_Name->dynamicCall("Select(void)"); //选中要选中的区域
pCustomer_Name->querySubObject("Range")->setProperty("Text", sText); //进行替换操作
}
//获取文档中名字为Name的标签
QAxObject *pName = document->querySubObject("Bookmarks(QVariant)", QString("Name"));
//选中标签,将字符sText插入到标签位置
if (!pName->isNull())
{
QString sText = "语文";//此处为替换内容
pName->dynamicCall("Select(void)"); //选中要选中的区域
pName->querySubObject("Range")->setProperty("Text", sText); //进行替换操作
}
//获取文档中名字为Number的标签
QAxObject *pNumber = document->querySubObject("Bookmarks(QVariant)", QString("Number"));
//选中标签,将字符sText插入到标签位置
if (!pNumber->isNull())
{
QString sText = "100";//此处为替换内容
pNumber->dynamicCall("Select(void)"); //选中要选中的区域
pNumber->querySubObject("Range")->setProperty("Text", sText); //进行替换操作
}
//将文件保存为doc,同样可以生成docx文档
QString pathsave = QApplication::applicationDirPath()+ "/template.docx";
document->dynamicCall("SaveAs(const QString&))", QDir::toNativeSeparators(pathsave));
document->dynamicCall("Close (boolean)", false);
word->dynamicCall("Quit()");
delete word;
return a.exec();
}
没有合适的资源?快使用搜索试试~ 我知道了~
Qt对word文档操作 com实例
共3个文件
pro:1个
dotx:1个
cpp:1个
5星 · 超过95%的资源 需积分: 5 5 下载量 84 浏览量
2024-01-17
15:17:08
上传
评论
收藏 12KB ZIP 举报
温馨提示
近期在使用Qt对word进行读写操作时候遇到很多问题,对于Qt内部没有很好的库函数可以使用;Qt/Windows桌面版提供了ActiveQt框架,用以为Qt和ActiveX提供完美结合。
资源推荐
资源详情
资源评论
收起资源包目录
QtWordTest.zip (3个子文件)
main.cpp 3KB
template.dotx 13KB
QtWordTest.pro 506B
共 3 条
- 1
资源评论
- 小姐不得无礼2024-04-10#完美解决问题
团子的高速列车
- 粉丝: 72
- 资源: 28
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功