QT Get Office file content

Source: Internet
Author: User

qt Get Office file content

You need to get the file contents of the Word file. Online for a long time, most of them are excel. And word is very few. So record here, convenient for everyone to check and use their own.

The qt version used is 5.4.2 .

Here's a code description:

    1. First include in the. Pro file

      QT + = Axcontainer

    2. Need to add the following header file

      #include <QAxWidget>

      #include <QAxObject>

    3. The detailed code is as follows



Void qt_word (Qstring filepath) {//Specifies the path to open the file     //qstring filepath =   "D:/doc/local.doc";     //create qaxwidget  objects that have objects for word      Qaxwidget *word=new qaxwidget ("Word.Application", &NBSP;0,&NBSP;QT::MSWINDOWSOWNDC);     //Sets whether word is visible, set to false here. This will not see Word's program     word->setproperty ("Visible",  false);     // Find its properties through the Word object,document    qaxobject * documents = word-> Querysubobject ("Documents");     //open the file to get the contents of the file through document     documents- >dynamiccall ("Open (QString)", FilePath);//    documents->dynamiccall ("Open (QString)", Qstring::fromlocal8bit ("D:/doc/local.doc"));     //gets the document     of the current activity Qaxobject *document = word->querysubobject ("ActiveDocument");     //from whenGet paragraphs    qaxobject *paragraphs = document-> in the document of the former activity Querysubobject ("paragraphs");     //loop input each 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;    &nbsP;   str = str.trimmed ();         qdebug () < <str;    }    //Close document    document-> Dynamiccall ("close  (Boolean)",  false);//     document->dynamiccall (" close  (Boolean) ",  false);     //exit word    word-> Dynamiccall ("Quit ()");}


Because the internal organization of Word is not very clear. So it was a little bit of groping out.

At present, only the word2003 have been tested. More than 2007 versions are not yet available. Hope to have a clear word within the organization of friends can point.

Reference from: 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

This article is from the "Do Your best" blog, so be sure to keep this source http://qiaopeng688.blog.51cto.com/3572484/1767288

QT Get Office file content

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.