Qt擷取office檔案內容

來源:互聯網
上載者:User

標籤:word處理 qt

       Qt擷取office檔案內容 

    需要擷取word檔案的檔案內容。網上找了好久,大部分都是excel的。而word的很少。所以在這裡記錄一下,方便大家查閱和自己使用。

使用的Qt版本是5.4.2 。

下面通過代碼說明:

  1. 首先在.pro檔案中加入

    QT       += axcontainer

  2. 需要加入以下標頭檔

    #include <QAxWidget>

    #include <QAxObject>

  3. 詳細代碼如下



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()<<str;    }    //關閉Document    document->dynamicCall("Close (boolean)", false);//     document->dynamicCall("Close (boolean)", false);    //退出word    word->dynamicCall("Quit()");}


由於對word的內部組織不是很清楚。所以都是一點點摸索出來。

目前只是對word2003進行了測試。2007以上版本還不好使。希望有清楚word內部組織的朋友能夠指點一下。

參考自:http://it-ua.info/news/2015/08/10/parsing-dokumenta-word-na-kartinki-abo-storya-pro-pereddiplomnih-budn.html

2016-04-24 19:21:44

本文出自 “做最好的自己” 部落格,請務必保留此出處http://qiaopeng688.blog.51cto.com/3572484/1767288

Qt擷取office檔案內容

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.