0

I have opened excel sheet in Qt and trying to get number of rows and columns filled (if all filled continuous) with this code :

QString file = QFileDialog::getOpenFileName(this,"Open file");

QAxWidget excel("Excel.Application");
excel.setProperty("Visible", true);

QAxObject * workbooks = excel.querySubObject("WorkBooks");
QAxObject* workbook = workbooks->querySubObject( "Open(const QString&)", file );
sheets = workbook->querySubObject( "Worksheets" );

QAxObject* sheet = sheets->querySubObject( "Item( int )", i );

QAxObject* rows = sheet->querySubObject( "Rows" );
int rowCount = rows->dynamicCall( "Count()" ).toInt(); 
QAxObject* columns = sheet->querySubObject( "Columns" );
int columnCount = columns->property("Count").toInt();
ui->label->setText(QString::number(rowCount)+" ," +QString::number(columnCount));

}

The problem is rowcount and columnCount are showing a large number but my sheet have 4x6 filled.

I have seen these questions : Get Last non Empty Cell of Excel Column Programatically

Finding the last filled row in Excel sheet in c#

but both are for C#

But I don't know how to do this in Qt

1 Answer 1

3

I have solved the problem with Excel.Range and UsedRange (it is not necessary that all cells should be continuously filled in this case in between empty cells will be counted)

QAxObject* sheet = sheets->querySubObject( "Item( int )", i );
QAxObject * range = sheet->querySubObject("UsedRange");
QAxObject * rows = range->querySubObject( "Rows" );
int rowCount = rows->dynamicCall( "Count()" ).toInt(); 
QAxObject* columns = range->querySubObject( "Columns" );
int columnCount = columns->property("Count").toInt();

ui->label->setText(QString::number(rowCount)+" ," +QString::number(columnCount));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.