Results 1 to 3 of 3

Thread: Read excel file

  1. #1
    Join Date
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Read excel file

    Hello forum,
    I have excel file with this pattern:
    Qt Code:
    1. ID #1 #2 #3
    2. NAME A B C
    3. #1 A x
    4. #2 B x
    5. #3 C x
    To copy to clipboard, switch view to plain text mode 
    "X" means that the row belongs to the column.
    Qt Code:
    1. QString path = fileName;
    2. sheet = nullptr;
    3. sheets = nullptr;
    4. workbook = nullptr;
    5. workbooks = nullptr;
    6. excelApplication = nullptr;
    7.  
    8. if(path == "")
    9. {
    10. qDebug() << "'fileName' is empty!";
    11. return;
    12. }
    13. path.replace("/", "\\");
    14.  
    15. excelApplication = new QAxObject( "Excel.Application", 0 );
    16.  
    17. if (excelApplication == nullptr)
    18. throw invalid_argument("Failed to initialize interop with Excel (probably Excel is not installed)");
    19.  
    20. excelApplication->dynamicCall( "SetVisible(bool)", false ); // hide excel
    21. excelApplication->setProperty( "DisplayAlerts", 0); // disable alerts
    22.  
    23. workbooks = excelApplication->querySubObject( "Workbooks" );
    24. workbook = workbooks->querySubObject( "Open(const QString&)", path );
    25. sheets = workbook->querySubObject( "Worksheets" );
    26.  
    27. ...
    28. ...
    29.  
    30. for(int i=0; i<rowsCnt; ;++i)
    31. for(int j=0; j<colsCnt; ;++j)
    32. {
    33. QAxObject* cell = sheet->querySubObject( "Cells( int, int )", i, j);
    34. QVariant value = cell->dynamicCall( "Value()" );
    35. dataRow.append(value);
    36. }
    To copy to clipboard, switch view to plain text mode 
    It works fine, but the execution time is slow, especially at a larger table (200x200 or more).

    My question is if exist faster way to read excel file.

    Thanks for any advice!

  2. #2
    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 excel file

    If you could find a way to replace the calls to querySubObject() and dynamicCall() in the inner loop over the cells, that would probably speed things up considerably.

    QAxObject is convenient, but if you can #import the Excel type libraries this would let you use the Excel COM objects directly without all the parsing overhead. Here's a link that might help.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Read excel file

    Thank you! I'll try the second option.

Similar Threads

  1. How can I Read Write Excel File
    By xingshaoyong in forum Qt Programming
    Replies: 6
    Last Post: 13th July 2011, 20:16
  2. problem to read excel file
    By keyurparekh in forum Qt Programming
    Replies: 4
    Last Post: 27th June 2011, 13:25
  3. How to read Excel file with Qt
    By HelloDan in forum Qt Programming
    Replies: 3
    Last Post: 13th May 2009, 20:27
  4. Read excel
    By psipsi in forum Qt Programming
    Replies: 2
    Last Post: 23rd December 2008, 17:07
  5. Read \Write Excel File using Qt
    By xingshaoyong in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 22: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.