Results 1 to 11 of 11

Thread: Excel and Qt

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Default Re: Excel and Qt

    Here's some code that can get you started. I must stress, that I don't fully understand it, since I copied and pasted it from somewhere else - and then started to screw around with it. So don't ask me to explain in depth. The comments in here are what I THINK happens.

    I still need to create a QAxWidget for some reason, even though I only want to read and not display the excel file. If anyone knows how I can get around using a QApplication and a QAxWidget object and still have this work: please let me know.

    Qt Code:
    1. #include <qaxobject>
    2. #include <qaxwidget>
    3. #include <qapplication>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QString strVal;
    8. const char* cp = "Range(E11)";
    9.  
    10. QApplication a(argc, argv);
    11. QAxWidget excel("Excel.Application");
    12.  
    13. // because I want to read the file - not display it.
    14. excel.setProperty("Visible", false);
    15.  
    16. QAxObject *workbooks = excel.querySubObject("WorkBooks");
    17. workbooks->dynamicCall("Open (const QString&)", QString("e:/tmp/test3.xls"));
    18. QAxObject *workbook = excel.querySubObject("ActiveWorkBook");
    19.  
    20. // I need data from the 2nd worksheet (worksheets are numbered starting from 1)
    21. QAxObject *worksheet = workbook->querySubObject("Worksheets(int)", 2);
    22.  
    23. // Read fom the second worksheet the contents of cell E11
    24. QAxObject *range = worksheet->querySubObject(cp);
    25. strVal = range->property("Value").toString();
    26.  
    27. // close the excel file
    28. excel.dynamicCall("Quit (void)");
    29.  
    30. return(0);
    31. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Excel and Qt

    Hi antialias,

    Thanks for the sample code. It worked almost right away. Just had to add "LIBS += qaxcontainer.lib" to my .pro file, and then use the full path name to my excel file.

    For the QAxWidget : you can just use a QAxObject instead of QAxWidget.

    For skipping QApplication, I don't know for sure. A 'console application' doesn't need QApplication. From "C++ Programming with Qt4' : "... since this is a console application, it has a slightly different .pro file from those we have seen for GUI applications. We only link against QtCore since we don't use any GUI fucntionallity. Then we specify that we want to enable console output on windows and that we don't want the application to live in a bundle on maxosx" and they give the following .pro file :
    TEMPLATE = app
    QT = core
    CONFIG += console
    CONFIG -= app_bundle
    SOURCES = ....... your sources

    By the way : if something goes wrong in the calls to the QAxObject (like specifying a wrong filename), you get detailed error information in the application output window of QtCreator. I hope you get this output too, because it helps a lot.

    Best regards,
    Marc

  3. #3
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Excel and Qt

    Hi antialias,

    Thanks for the sample code. It worked almost right away. Just had to add "LIBS += qaxcontainer.lib" to my .pro file, and then use the full path name to my excel file.

    For the QAxWidget : you can just use a QAxObject instead of QAxWidget.

    For skipping QApplication, I don't know for sure. A 'console application' doesn't need QApplication. From "C++ Programming with Qt4' : "... since this is a console application, it has a slightly different .pro file from those we have seen for GUI applications. We only link against QtCore since we don't use any GUI fucntionallity. Then we specify that we want to enable console output on windows and that we don't want the application to live in a bundle on maxosx" and they give the following .pro file :
    TEMPLATE = app
    QT = core
    CONFIG += console
    CONFIG -= app_bundle
    SOURCES = ....... your sources

    By the way : if something goes wrong in the calls to the QAxObject (like specifying a wrong filename), you get detailed error information in the application output window of QtCreator. I hope you get this output too, because it helps a lot.

    Best regards,
    Marc

  4. #4

    Default Re: Excel and Qt

    For the QAxWidget : you can just use a QAxObject instead of QAxWidget.
    When I try that it crashes in this line.
    Qt Code:
    1. workbooks->dynamicCall("Open (const QString&)", QString("e:/tmp/test3.xls"));
    To copy to clipboard, switch view to plain text mode 
    with a Null pointer exception.
    If I leave out the QApplication it doesn't compile at all (even though my app is a console application)

    I'm working with Visual Studio 2010 Express (just using Qt classes/libs but not making a GUI for this project) - so I don't have a pro-File. The debug information is somewhat limited, too :-(

    But it works well enough so I'll move on to other things...

  5. #5
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    518
    Thanks
    13
    Thanked 77 Times in 75 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Excel and Qt

    Hi, for console applications there is QCoreApplication.

    Ginsengelf

Similar Threads

  1. Trying to export to Excel
    By ShamusVW in forum Qt Programming
    Replies: 7
    Last Post: 4th August 2010, 08:07
  2. Read excel
    By psipsi in forum Qt Programming
    Replies: 2
    Last Post: 23rd December 2008, 17:07
  3. Automating Excel 97
    By Aki-Matti in forum Qt Programming
    Replies: 0
    Last Post: 14th December 2007, 06:28
  4. ActiveQt + Excel
    By nile.one in forum Qt Programming
    Replies: 7
    Last Post: 19th October 2007, 22:58
  5. Excel using Qt
    By nmn in forum Qt Programming
    Replies: 3
    Last Post: 28th July 2007, 02:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.