Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Question about link with Excel file
QtWS25 Last Chance

Question about link with Excel file

Scheduled Pinned Locked Moved Solved General and Desktop
qaxobjectexcel
5 Posts 3 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MartinChan
    wrote on last edited by MartinChan
    #1

    Recently I just want to load excel into Qt,and i used to have a post in Qt Forum,it didn't work out until now.
    However,I found some code on the Internet says users can use QAxObject to solve this question,the code is like:

    QAxObject excel("Excel.Application");  
    

    It always starts like this,but I never succeed in this way,it reminds me it can't find any "Excel.Application" thing.What's more,I have checked my QAxObject contents,but I didn't find anything referred to "Excel.Application".

    So anybody can help me about the Excel thing,after failure in ODBC and QAxObject,I doubted if there is some problem with my Excel Version or MS Office Software.@mrjj

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Why not just test your excel?
      You can try this sample and see.
      Works here so should work at your place too.
      https://www.dropbox.com/s/kxpei9wmx0v95rx/excel.zip?dl=0

      M 1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        If you need to read/write xlsx files you could try QtXlsxWriter. It does not require excel installed and performs all the basic tasks

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2
        • mrjjM mrjj

          Hi
          Why not just test your excel?
          You can try this sample and see.
          Works here so should work at your place too.
          https://www.dropbox.com/s/kxpei9wmx0v95rx/excel.zip?dl=0

          M Offline
          M Offline
          MartinChan
          wrote on last edited by
          #4

          @mrjj Sorry to interrupt you for such a long time:(

          Now it can work(using QAxObject "Excel.Application" ),it can open a file and read back a caption ,but when I use DynamicCall() function,it usually reminds me of some mistakes(can't find the fun in the activce), I thought it could be related to the new version of excel and the function under activex has changed(I am a rookie...:()And should I find the VBA book to learn the newest operation of VBA?

          And I can't open the dropbox link... :( Maybe it's time to buy a VPN...

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MartinChan
            wrote on last edited by
            #5

            Finally Solved.Luckily I found some posts told the reason----It seems the qt has to initialize and release OLE in a sub Thread(the GUI Thread has done this job so sometimes it may not show errors)

            Here is the code:

            #include <QCoreApplication>
            #include <QAxObject>
            #include "qt_windows.h"               //Necessary
            #include <QVariant>
            #include <QDebug>
            
            int main(int argc, char *argv[])
            {
                //Initialize OLE
                HRESULT r = OleInitialize(0);
                if (r != S_OK && r != S_FALSE)
                {qWarning("OLE initialize failure(error %x)",(unsigned int)r);}
            
                QCoreApplication a(argc, argv);
                QAxObject excel("Excel.Application");
                excel.setProperty("Visible",false);
                QAxObject *workbooks = excel.querySubObject("WorkBooks");
                workbooks->dynamicCall("Open (const QString&)",QString("d:/grade.xls"));
                QAxObject *workbook = excel.querySubObject("ActiveWorkBook");
                QAxObject *worksheets = workbook->querySubObject("WorkSheets");
                QAxObject *worksheet=worksheets->querySubObject("Item(int)",1);
                QAxObject *range;
                QString strVal;
                QAxObject *usedrange=worksheet->querySubObject("UsedRange");
                int endrow=usedrange->querySubObject("Rows")->property("Count").toInt();
                int endcolumn=usedrange->querySubObject("Columns")->property("Count").toInt();
                for (int i=1;i<=endrow;i++){
                    for (int j=1;j<=endcolumn;j++){
                        range=worksheet->querySubObject("Cells(int,int)",i,j);
                        strVal=range->dynamicCall("Value2()").toString();
                        qDebug()<<strVal;
                    }
                    qDebug()<<endl;
                }
            
                workbook->dynamicCall("Close()");
                excel.dynamicCall("Quit()");
            
                OleUninitialize();
            
                return a.exec();
            }
            
            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved