入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

Qt5编程——word操作

创建时间:2016-09-26 投稿人: 浏览次数:2404
	QDir dir;
	QString srcPath, dstPath;
	//新建一个word应用程序,并设置为不可见
	QAxWidget *word = new QAxWidget("Word.Application");
	word->setProperty("Visible", false);

	//获取所有的工作文档
	QAxObject *documents = new QAxObject;
	documents = word->querySubObject("Documents");

	//新建一个文档
	//documents->dynamicCall("Add(void)");

	//以ReportTemplate.docx为模版新建一个文档
	srcPath = dir.currentPath() + "/docs/ReportTemplate.docx";
	documents->dynamicCall("Add(QString)", srcPath);

	//获取当前激活的文档
	QAxObject *document = word->querySubObject("ActiveDocument");

	//获取文档中的标签并写入
	QAxObject *bookmark_code;
	QAxObject *selection;
	bookmark_code = document->querySubObject("Bookmarks(QVariant)", "Name");//Name是word文档中的标签名
	selection = word->querySubObject("Selection");
	bookmark_code->querySubObject("Range")->setProperty("Text", querySelected.name);

	bookmark_code = document->querySubObject("Bookmarks(QVariant)", "Age");
	selection = word->querySubObject("Selection");
	bookmark_code->querySubObject("Range")->setProperty("Text", reportAge);

	bookmark_code = document->querySubObject("Bookmarks(QVariant)", "Sex");
	selection = word->querySubObject("Selection");
	bookmark_code->querySubObject("Range")->setProperty("Text", reportSex);

	bookmark_code = document->querySubObject("Bookmarks(QVariant)", "Time");
	selection = word->querySubObject("Selection");
	bookmark_code->querySubObject("Range")->setProperty("Text", reportTime);

	bookmark_code = document->querySubObject("Bookmarks(QVariant)", "Assessment");
	selection = word->querySubObject("Selection");
	bookmark_code->querySubObject("Range")->setProperty("Text", reportAssess);

	bookmark_code = document->querySubObject("Bookmarks(QVariant)", "Number");
	selection = word->querySubObject("Selection");
	bookmark_code->querySubObject("Range")->setProperty("Text", reportID);

	bookmark_code = document->querySubObject("Bookmarks(QVariant)", "IPAddr");
	selection = word->querySubObject("Selection");
	bookmark_code->querySubObject("Range")->setProperty("Text", querySelected.ipAndPort);

	bookmark_code = document->querySubObject("Bookmarks(QVariant)", "Operator");
	selection = word->querySubObject("Selection");
	bookmark_code->querySubObject("Range")->setProperty("Text", querySelected.username);

	//写入“hello world”
	//selection = word->querySubObject("Selection");
	//selection->dynamicCall("TypeText(const QString&)", "hello world");

	//设置保存
	dstPath = dir.currentPath() + "/" + QString::fromLocal8Bit("测试报告")+".doc";
	QVariant newFileName(dstPath);//保存路径及名称
	QVariant fileFormat(1);//文件格式
	document->dynamicCall("SaveAs(const QVariant&, const QVariant&)", newFileName, fileFormat);

	document->dynamicCall("Close (boolean)", true);
	word->dynamicCall("Quit(void)");

声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像