Results 1 to 11 of 11

Thread: Excel and Qt

  1. #1

    Default Excel and Qt

    So I've been browsing for how to read / write excel files with Qt...I only found this one example:

    Qt Code:
    1. QAxObject* excel = new QAxObject( "Excel.Application", 0 );
    2. // excel->dynamicCall("SetVisible(bool)",true);
    3. QAxObject *workbooks = excel->querySubObject( "Workbooks" );
    4. QAxObject *workbook = workbooks->querySubObject( "Open(const QString&)", "e:/tmp/test.xls" );
    5. QAxObject *sheets = workbook->querySubObject( "Worksheets" );
    6. int count = sheets->dynamicCall("Count()").toInt();
    7. QStringList sheetsList;
    8. for (int i=1; i<=count; i++)
    9. {
    10. QAxObject* sheet = sheets->querySubObject( "Item( int )", i );
    11. QString name = sheet->dynamicCall("Name()").toString();
    12. sheetsList.append(name);
    13. }
    14. workbook->dynamicCall("Close()");
    15. excel->dynamicCall( "Quit()");
    To copy to clipboard, switch view to plain text mode 

    compiles fine, but it crashes on execution on the line
    Qt Code:
    1. QAxObject *workbook = workbooks->querySubObject( "Open(const QString&)", "e:/tmp/test.xls" );
    To copy to clipboard, switch view to plain text mode 
    (yes, the excel file does exist and resides in that directory. Replacing the / with \\ doesn't solve the problem)

    what am I doing wrong?

  2. #2
    Join Date
    Dec 2009
    Posts
    18
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Excel and Qt

    I'm only a noob but I think you have found only a piece of a bigger example......

    I had the same problem in the past (write to an excel) and I didn't find anything free of cost so I did a very basic XML writer (excel 2003, fully compatible also with OpenOffice) starting from this :

    http://www.qtforum.de/forum/viewtopi...d09c193368953b

    You can do the same !!

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Excel and Qt

    "Crashes" is a fairly broad statement. Is excel or workbooks equal to zero?

  4. #4

    Default Re: Excel and Qt

    OK, found an example that works. Since I only need to read from an excel file I could throw out a lot of stuff. Only thing I'm not sure about is how to close the file without it asking me if I want to save (since I will need to process a lot of excel files in rapid succession automatically)

    but that is only a minor inconvenience.

  5. #5
    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

    Hello,

    Could you post (al link to) the example ?

    I need to do just the same.

    Best regards,
    Marc

  6. #6

    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 

  7. #7
    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

  8. #8
    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

  9. #9

    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...

  10. #10
    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

  11. #11
    Join Date
    Oct 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Cool Re: Excel and Qt

    Make sure that your .pro contains the below lines..
    LIBS += -lqaxserver \
    -lqaxcontainer
    My advice is to start a new class and do the code..You can connect the code to a pushbutton slot...

    QAxObject *excel=new QAxObject("Excel.Application");
    excel->setProperty("Visible",false);
    QAxObject *workbooks=excel->querySubObject("WorkBooks");
    workbooks->dynamicCall("Open (const QString&)",QString("C:\\Book.xls"));
    QAxObject *workbook=excel->querySubObject("ActiveWorkBook");
    QAxObject *worksheets=workbook->querySubObject("WorkSheets");

    U can contact me at aneeshkumaru@yahoo.co.in ...i also got a lot of issue..bcz i am also a beginner. but i solved most of the issues...
    Last edited by aneeshkumaru; 29th October 2011 at 11:56. Reason: updated contents

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.