Using ActiveX Object in Qt

From Qt Wiki
(Redirected from Using ActiveX Object in QT)
Jump to navigation Jump to search

While working on an assignment I was told to read the Excel and read an Image (OLE) object in excel. I started searching in the web and got some snippets to read the excel sheet. But no tutorials/wiki were available on this subject. So thought I will add this page here.

Here is the snippet which will be used to read the Excel sheet. Found it in one of the QT thread.

    QAxObject* excel = new QAxObject( "Excel.Application", 0 );
    QAxObject* workbooks = excel->querySubObject( "Workbooks" );
    QAxObject* workbook = workbooks->querySubObject( "Open(const QString&)", "C:\\1.xls" );
    QAxObject* sheets = workbook->querySubObject( "Worksheets" );
    QList<QVariantList> data; //Data list from excel, each QVariantList is worksheet row
 
    //worksheets count
    int count = sheets->dynamicCall("Count()").toInt();
 
    count = sheets->property("Count").toInt();
    for (int i=1; i<=count; i++) //cycle through sheets
     {
        //sheet pointer
        QAxObject* sheet = sheets->querySubObject( "Item( int )", i );
 
        QAxObject* rows = sheet->querySubObject( "Rows" );
        int rowCount = rows->dynamicCall( "Count()" ).toInt(); //unfortunately, always returns 255, so you have to check somehow validity of cell values
        QAxObject* columns = sheet->querySubObject( "Columns" );
        int columnCount = columns->property("Count").toInt();
 
 
 
 
        for (int row=1; row <= rowCount; row++)
        {
            QVariantList dataRow;
            bool isEmpty = true; //when all the cells of row are empty, it means that file is at end (of course, it maybe not right for different excel files. it's just criteria to calculate somehow row count for my file)
            for (int column=1; column <= columnCount; column++)
            {
                //Do something usefule here
            }
            if (isEmpty) //criteria to get out of cycle
                break;
            data.append(dataRow);
 
        }
    }
 
    workbook->dynamicCall("Close()");
    excel->dynamicCall("Quit()");

But my main problem was finding out the API of sheet QAX object to get the relative information like row count ,column count etc.

There is a API in QAXBASe class which generates the API documentation for the corresponding QAxObject . Which can be redirected to a file by using the sample code as below.

 QFile file1("c://sheet2.html");
    file1.open(QIODevice::WriteOnly | QIODevice::Text);
    QTextStream out(&file1);
    out << excel->generateDocumentation();
    file1.close();

Depending on the QAxObject type the generated API doc will be different and can be used for finding out the statndard APIs.

For an example after getting the handler to sheet I was not knowing the API to use to get the pictures used in the current sheet. So I generated API doc using

generatedocumentaion()

API and found that I can use

sheet->querySubObject( "Pictures" )

to get all the pictures used in that sheet.