Results 1 to 7 of 7

Thread: Read/Write with Excel in Qt

  1. #1
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Read/Write with Excel in Qt

    Hi all...

    In Qt5 what are the best and easiest ways to read/write with excel?

    Note: I've seen the doc about it and something about ActiveX and QAxContainer and this:

    http://www.qtcentre.org/threads/4214...in-excel-files

    I am quit sure about QAxObject and its friends that will work for me But i don't know if is there any better ways for it or not? And if this modules

    can work with spreadsheets in unix??? I just want to know if someone have experienced with it can guide me

    Thanks

  2. #2
    Join Date
    Sep 2012
    Location
    Iran, Tehran
    Posts
    76
    Thanks
    17
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Read/Write with Excel in Qt

    I haven't tried myself but I think you can use ODBC and QSqlTableModel,
    I don't know whether it works in unix but at least it should work in windows.
    The following may help to find the appropriate connection string:
    http://www.connectionstrings.com/excel-2007

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Read/Write with Excel in Qt

    can work with spreadsheets in unix???
    No, unless you can find some portable library for working with Excel files. ActiveX and QAxContainer are Windows-only, as are any ODBC or any other drivers supplied by Microsoft.

    It is not at all clear whether these drivers will work with a "free-form" Excel spreadsheet, or whether they require the spreadsheet to be structured (in other words, a table of rows and columns like a database table).

  4. The following user says thank you to d_stranz for this useful post:

    alizadeh91 (12th March 2013)

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Read/Write with Excel in Qt

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    d_stranz (13th March 2013)

  7. #5
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Read/Write with Excel in Qt

    Thanks a lot wysota and ashkan got it

  8. #6
    Join Date
    Oct 2009
    Location
    Maryland
    Posts
    16
    Thanks
    3
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Read/Write with Excel in Qt

    I've seen Qt (activeQt) code samples that try to count the cells in an Excel ActiveX object. I wanted to post an example that uses the "UsedRange" query in the hopes that folks will find this. Simpler than iterating over the data trying to guess which cells are used. This is a simple class derived from QTableWidget that can import an Excel sheet.

    Qt Code:
    1. #include <QTableWidget>
    2. #include <QAxObject>
    3. #include <QTableWidgetItem>
    4.  
    5. class ExcelTable : public QTableWidget
    6. {
    7. Q_OBJECT
    8. public:
    9. explicit ExcelTable(QWidget *parent = 0) : QTableWidget(parent) {}
    10. ~ExcelTable() {}
    11. public slots:
    12. void import(QString fileName, int sheetNumber=1) {
    13. QAxObject* excel = new QAxObject( "Excel.Application", 0 );
    14. QAxObject* workbooks = excel->querySubObject( "Workbooks" );
    15. QAxObject* workbook = workbooks->querySubObject( "Open(const QString&)", fileName );
    16. QAxObject* sheets = workbook->querySubObject( "Worksheets" );
    17. int sheetCount = sheets->dynamicCall("Count()").toInt(); //worksheets count
    18. QAxObject* sheet = sheets->querySubObject( "Item( int )", sheetNumber );
    19.  
    20. // Find the cells that actually have content
    21. QAxObject* usedrange = sheet->querySubObject( "UsedRange");
    22. QAxObject * rows = usedrange->querySubObject("Rows");
    23. QAxObject * columns = usedrange->querySubObject("Columns");
    24. int intRowStart = usedrange->property("Row").toInt();
    25. int intColStart = usedrange->property("Column").toInt();
    26. int intCols = columns->property("Count").toInt();
    27. int intRows = rows->property("Count").toInt();
    28.  
    29. // replicate the Excel content in the QTableWidget
    30. this->setColumnCount(intColStart+intCols);
    31. this->setRowCount(intRowStart+intRows);
    32. for (int row=intRowStart ; row < intRowStart+intRows ; row++) {
    33. for (int col=intColStart ; col < intColStart+intCols ; col++) {
    34. QAxObject* cell = sheet->querySubObject( "Cells( int, int )", row, col );
    35. QVariant value = cell->dynamicCall( "Value()" );
    36. if (value.toString().isEmpty())
    37. continue;
    38.  
    39. QTableWidgetItem * twi = new QTableWidgetItem(value.toString());
    40. this->setItem(row-1, col-1, twi);
    41. }
    42. }
    43.  
    44. // clean up and close up
    45. workbook->dynamicCall("Close()");
    46. excel->dynamicCall("Quit()");
    47. }
    48. };
    To copy to clipboard, switch view to plain text mode 

  9. The following 2 users say thank you to iraytrace for this useful post:

    d_stranz (7th April 2015), mfurqan2202 (15th July 2018)

  10. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Read/Write with Excel in Qt

    Thanks for a good example. Even more general would be to import into a QAbstractItemModel or QAbstractTableModel so you could have multiple views of the same data if desired.

Similar Threads

  1. How can I Read Write Excel File
    By xingshaoyong in forum Qt Programming
    Replies: 6
    Last Post: 13th July 2011, 21:16
  2. read and write data in excel files
    By robotics in forum Qt Programming
    Replies: 3
    Last Post: 6th June 2011, 01:13
  3. Replies: 2
    Last Post: 2nd November 2010, 06:15
  4. Read excel
    By psipsi in forum Qt Programming
    Replies: 2
    Last Post: 23rd December 2008, 18:07
  5. Read \Write Excel File using Qt
    By xingshaoyong in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 23:07

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.