Qt World Summit: Register Today!

QAxObject Excel

  • Hi,
    I am newbie to using QAxObject.
    My problem is I know how to use the find method, but I don't know how to use the row and column values I get with the find method in the for loop.

    For example;
    First, I need to find out which column the number information is in. Then i'll get the numbers and l take the values corresponding to the numbers.


     QString noStr = "No";
     QAxObject* findNoStr = range->querySubObject("Find(const QString&)", noStr );   // ForExample (&B&2)  
     for (int row = 1; row <= /*findNoStr.row*/; row++)
         // Something like this
         QAxObject* value = sheet->querySubObject("Cells(int,int)", row ,/*findNoStr.column */);
         numbers.pushback(value ->dynamicCall("Value()").toInt())

  • @Ceng0
    This would be better asked in an Excel forum, it is not connected to Qt.

    But if you are saying you get back a string like B2, or similar, but you have to call something else with a row and column number, then just turn the "B2" into its row/column in your code. (I actually have a feeling there are Excel utility methods to convert between A1-format and row-column-numbers, but it may be just as easy to do it yourself.)

  • If you want to read any excel file then use QSqlDatabase For It

    my personal experience saying that QAxObject take Lot's of time for read any kind of Excel file
    And QAxObject Very complicated Concept

    you can see my code for Read Any Kind Of Excel File

    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", "xlsx_connection");
        db.setDatabaseName("DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" + QString(FilePath) + "");
            qDebug() << "Excel Is Connected";
            qDebug() << "Excel Is Not Connected";
        QSqlQuery query(db);
        query.prepare("SELECT * FROM [" + QString("Sheet1") + "$]"); // Select range, place A1:B5 after $
            while (query.next())
                       ///// Your Code

    for this You must have to Install Mysql Driver For Excel

  • @Ketan__Patel__0011
    This is interesting, thanks for posting it.

    But I think it's only right to point out that what you get back, and what you can do with it, is very different from the VBA approach.

  • @JonB

    You can see OP Is shared her one image there he try to read All Rows One By One

        QSqlQuery query(db);
        query.prepare("SELECT * FROM [" + QString("Sheet1") + "$]")
            while (query.next())
                       QString column1 = query.value(0).toString(); /// Column 1 Value
                       QString column2 = query.value(1).toString(); /// Column 1 Value
                       QString column3 = query.value(2).toString(); /// Column 1 Value

    According to the my code
    all columns value are store in specific variable one by one
    You can store all are the values in Array Or List

    Or If You want to read any single value then just provide it Range in Query
    Because Excel Is working on Range Based (Range Based Structure)

Log in to reply