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. How can i read an Excel worksheet from Qt application?
Forum Updated to NodeBB v4.3 + New Features

How can i read an Excel worksheet from Qt application?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 18.8k 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.
  • E Offline
    E Offline
    Eduardo12l
    wrote on 25 Jun 2017, 02:19 last edited by Eduardo12l
    #1

    I read the Qt documentation about it but it does not specify so much.
    I know that ActiveX and QODBC are some ways.
    But i read that ActiveX can be gotten in commercial Qt and QODBC ,according to Qt link (http://wiki.qt.io/Handling_Microsoft_Excel_file_format), has some bugs or problems.
    Is there any other way?

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hskoglund
      wrote on 25 Jun 2017, 03:33 last edited by
      #2

      Hi, I wrote an app before that reads Excel files, but it requires Excel (or Microsoft Office) to be installed.

      Do you need to read Excel worksheets from a Qt application on a PC without Microsoft Office or Excel installed? If not, then it's pretty easy, you just "remote-control" Excel using ActiveQt,

      E 1 Reply Last reply 25 Jun 2017, 18:03
      6
      • H hskoglund
        25 Jun 2017, 03:33

        Hi, I wrote an app before that reads Excel files, but it requires Excel (or Microsoft Office) to be installed.

        Do you need to read Excel worksheets from a Qt application on a PC without Microsoft Office or Excel installed? If not, then it's pretty easy, you just "remote-control" Excel using ActiveQt,

        E Offline
        E Offline
        Eduardo12l
        wrote on 25 Jun 2017, 18:03 last edited by Eduardo12l
        #3

        Hi, @hskoglund many thank for replying. How can i remote-control an Excel using ActiveQt?

        1 Reply Last reply
        2
        • H Offline
          H Offline
          hskoglund
          wrote on 26 Jun 2017, 05:35 last edited by
          #4

          Hi try something like this:

          create a simple Excel test file, say C:\Temp\Test.xslx" which contains prime numbers in column A, eg. "3" "5" "7" "11" etc. (one in each row).

          Then create an empty Widgets app, in the .pro file add "axcontainer":
          QT += core gui axcontainer
          add #include "qaxobject.h" and #include "qdebug.h" in mainwindow.cpp
          and then add this beautiful code in mainwindow.cpp after "ui->setupUi(this)";

          auto excel     = new QAxObject("Excel.Application");
          auto workbooks = excel->querySubObject("Workbooks");
          auto workbook  = workbooks->querySubObject("Open(const QString&)","c:\\temp\\test.xlsx");
          auto sheets    = workbook->querySubObject("Worksheets");
          auto sheet     = sheets->querySubObject("Item(int)", 1);
          
          // read the first cells in row 1..5
          for (int r = 1; (r <= 5); ++r)
          {
              auto cCell = sheet->querySubObject("Cells(int,int)",r,1);
              qDebug() << cCell->dynamicCall("Value()").toInt();
          }
          

          If all goes well you should see this in Qt Creator:

          Starting C:\Projects\build-ExcelTest-Desktop_Qt_5_9_0_MSVC2015_32bit-Release\release\ExcelTest.exe...
          3
          5
          7
          11
          13
          

          Good luck!

          E 1 Reply Last reply 27 Jun 2017, 02:35
          7
          • V Offline
            V Offline
            VRonin
            wrote on 26 Jun 2017, 08:20 last edited by
            #5

            If you can't require Excel installed see https://github.com/VSRonin/QtXlsxWriter

            "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

            E 1 Reply Last reply 27 Jun 2017, 05:19
            4
            • H hskoglund
              26 Jun 2017, 05:35

              Hi try something like this:

              create a simple Excel test file, say C:\Temp\Test.xslx" which contains prime numbers in column A, eg. "3" "5" "7" "11" etc. (one in each row).

              Then create an empty Widgets app, in the .pro file add "axcontainer":
              QT += core gui axcontainer
              add #include "qaxobject.h" and #include "qdebug.h" in mainwindow.cpp
              and then add this beautiful code in mainwindow.cpp after "ui->setupUi(this)";

              auto excel     = new QAxObject("Excel.Application");
              auto workbooks = excel->querySubObject("Workbooks");
              auto workbook  = workbooks->querySubObject("Open(const QString&)","c:\\temp\\test.xlsx");
              auto sheets    = workbook->querySubObject("Worksheets");
              auto sheet     = sheets->querySubObject("Item(int)", 1);
              
              // read the first cells in row 1..5
              for (int r = 1; (r <= 5); ++r)
              {
                  auto cCell = sheet->querySubObject("Cells(int,int)",r,1);
                  qDebug() << cCell->dynamicCall("Value()").toInt();
              }
              

              If all goes well you should see this in Qt Creator:

              Starting C:\Projects\build-ExcelTest-Desktop_Qt_5_9_0_MSVC2015_32bit-Release\release\ExcelTest.exe...
              3
              5
              7
              11
              13
              

              Good luck!

              E Offline
              E Offline
              Eduardo12l
              wrote on 27 Jun 2017, 02:35 last edited by Eduardo12l
              #6

              Hi @hskoglund your way works almost Perfect!! Thx, but i have only one problem: I have to search an specific word in my Excel worksheet that have 700 Rows and 10 Columns and it takes around 30 seconds in process all the worksheet ¿do you know what can i do? And sometimes i dont know why it opens a window where says : "file available" its ready to be written and read or something like it but once it opened 16 windows with that phrase

              H 1 Reply Last reply 27 Jun 2017, 06:04
              1
              • V VRonin
                26 Jun 2017, 08:20

                If you can't require Excel installed see https://github.com/VSRonin/QtXlsxWriter

                E Offline
                E Offline
                Eduardo12l
                wrote on 27 Jun 2017, 05:19 last edited by
                #7

                Hi @VRonin thx you. Your way looks very very complex Im novice in C++ and Qt

                1 Reply Last reply
                1
                • E Eduardo12l
                  27 Jun 2017, 02:35

                  Hi @hskoglund your way works almost Perfect!! Thx, but i have only one problem: I have to search an specific word in my Excel worksheet that have 700 Rows and 10 Columns and it takes around 30 seconds in process all the worksheet ¿do you know what can i do? And sometimes i dont know why it opens a window where says : "file available" its ready to be written and read or something like it but once it opened 16 windows with that phrase

                  H Offline
                  H Offline
                  hskoglund
                  wrote on 27 Jun 2017, 06:04 last edited by
                  #8

                  @Eduardo12l about that message "file available" and Excel opening lots of windows in the background, that's most likely because my example above was sloppy and didn't close Excel when done, so that Excel became an zombie in the background for you :-(

                  I've made a new example, it closes Excel at the end, should cause less zombie activity:
                  To search for a word in your Excel worksheet, you can use the "Find" call, I've modified my Excel test.xlsx file like this, filling 700 rows and 10 columns with words:
                  Test file

                  and this is the new code:

                  auto excel     = new QAxObject("Excel.Application");
                  auto workbooks = excel->querySubObject("Workbooks");
                  auto workbook  = workbooks->querySubObject("Open(const QString&)","c:\\temp\\test.xlsx");
                  auto sheets    = workbook->querySubObject("Worksheets");
                  auto sheet     = sheets->querySubObject("Item(int)", 1);    // use first worksheet
                  
                  // setup a range of 700 rows and 10 columns
                  auto range     = sheet->querySubObject("Range(A1,J700)");
                  
                  // find a word
                  QString word = "engaged";
                  auto find = range->querySubObject("Find(const QString&)",word);
                  
                  // if successful, show the address (first cell found)
                  if (nullptr != find)
                      qDebug() << find->dynamicCall("Address");
                  
                  // don't forget to quit Excel
                  excel->dynamicCall("Quit()");
                  delete excel;
                  

                  Using the "Find" call is pretty fast, should only take a few seconds, this is what Qt Creator should show:

                  Starting C:\Projects\build-ExcelTest-Desktop_Qt_5_9_0_MSVC2015_32bit-Release\release\ExcelTest.exe...
                  QVariant(QString, "$E$4")```
                  E 1 Reply Last reply 28 Jun 2017, 02:06
                  6
                  • H hskoglund
                    27 Jun 2017, 06:04

                    @Eduardo12l about that message "file available" and Excel opening lots of windows in the background, that's most likely because my example above was sloppy and didn't close Excel when done, so that Excel became an zombie in the background for you :-(

                    I've made a new example, it closes Excel at the end, should cause less zombie activity:
                    To search for a word in your Excel worksheet, you can use the "Find" call, I've modified my Excel test.xlsx file like this, filling 700 rows and 10 columns with words:
                    Test file

                    and this is the new code:

                    auto excel     = new QAxObject("Excel.Application");
                    auto workbooks = excel->querySubObject("Workbooks");
                    auto workbook  = workbooks->querySubObject("Open(const QString&)","c:\\temp\\test.xlsx");
                    auto sheets    = workbook->querySubObject("Worksheets");
                    auto sheet     = sheets->querySubObject("Item(int)", 1);    // use first worksheet
                    
                    // setup a range of 700 rows and 10 columns
                    auto range     = sheet->querySubObject("Range(A1,J700)");
                    
                    // find a word
                    QString word = "engaged";
                    auto find = range->querySubObject("Find(const QString&)",word);
                    
                    // if successful, show the address (first cell found)
                    if (nullptr != find)
                        qDebug() << find->dynamicCall("Address");
                    
                    // don't forget to quit Excel
                    excel->dynamicCall("Quit()");
                    delete excel;
                    

                    Using the "Find" call is pretty fast, should only take a few seconds, this is what Qt Creator should show:

                    Starting C:\Projects\build-ExcelTest-Desktop_Qt_5_9_0_MSVC2015_32bit-Release\release\ExcelTest.exe...
                    QVariant(QString, "$E$4")```
                    E Offline
                    E Offline
                    Eduardo12l
                    wrote on 28 Jun 2017, 02:06 last edited by Eduardo12l
                    #9

                    Hello @hskoglund definitely it works very well but it takes 17 seconds ¿do you know what can i do to reduce that time it takes ?

                    H 1 Reply Last reply 28 Jun 2017, 13:30
                    1
                    • E Eduardo12l
                      28 Jun 2017, 02:06

                      Hello @hskoglund definitely it works very well but it takes 17 seconds ¿do you know what can i do to reduce that time it takes ?

                      H Offline
                      H Offline
                      hskoglund
                      wrote on 28 Jun 2017, 13:30 last edited by
                      #10

                      @Eduardo12l Hmm 17 seconds... For one Find() call? If you start Excel, and do a Find inside it (ctrl-F) does it also take 17 seconds?

                      E 1 Reply Last reply 29 Jun 2017, 00:05
                      3
                      • H hskoglund
                        28 Jun 2017, 13:30

                        @Eduardo12l Hmm 17 seconds... For one Find() call? If you start Excel, and do a Find inside it (ctrl-F) does it also take 17 seconds?

                        E Offline
                        E Offline
                        Eduardo12l
                        wrote on 29 Jun 2017, 00:05 last edited by
                        #11

                        @hskoglund I reduced the size of my excel to 2 rows and it takes 5 seconds i think my Qt application has problems when tries to open Excel.

                        1 Reply Last reply
                        0

                        1/11

                        25 Jun 2017, 02:19

                        • Login

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