本文共 1478 字,大约阅读时间需要 4 分钟。
Qt获取office文件内容
需要获取word文件的文件内容。网上找了好久,大部分都是excel的。而word的很少。所以在这里记录一下,方便大家查阅和自己使用。
使用的Qt版本是5.4.2 。
下面通过代码说明:首先在.pro文件中加入
QT += axcontainer
需要加入以下头文件
#include
#include
详细代码如下
void Qt_word(QString filePath)
{
//指定要打开文件的路径
//QString filePath = "D:/doc/local.doc";
//创建QAxWidget 对象,使其拥有Word的对象
QAxWidget *word=new QAxWidget("Word.Application", 0, Qt::MSWindowsOwnDC);
//设置word是否为可见,这里设置为false。这样就不会看到word的程序了
word->setProperty("Visible", false);
//通过word对象找到它的属性,Document
QAxObject * documents = word->querySubObject("Documents");
//通过Document打开要获取文件内容的文件
documents->dynamicCall("Open(QString)",filePath);
// documents->dynamicCall("Open(QString)",QString::fromLocal8Bit("D:/doc/local.doc"));
//获取当前活动的Document
QAxObject *document = word->querySubObject("ActiveDocument");
//从当前活动的Document中获取paragraphs
QAxObject *paragraphs = document->querySubObject("Paragraphs");
//循环输入每一个paragraph
for (int ipar = 1; ipar <= paragraphs->property("Count").toInt(); ipar++)
{
QAxObject *lines = paragraphs->querySubObject("Item(QVariant)", ipar);
QAxObject *line = lines->querySubObject("Range");
QString str = line->property("Text").toString();
line->clear();
delete line;
lines->clear();
delete lines;
str = str.trimmed();
qDebug()<
}
//关闭Document
document->dynamicCall("Close (boolean)", false);
// document->dynamicCall("Close (boolean)", false);
//退出word
word->dynamicCall("Quit()");
}
由于对word的内部组织不是很清楚。所以都是一点点摸索出来。
目前只是对word2003进行了测试。2007以上版本还不好使。希望有清楚word内部组织的朋友能够指点一下。
2016-04-24 19:21:44
转载地址:https://blog.csdn.net/weixin_33921444/article/details/116812748 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!