0
votes

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

1 Answers

3
votes

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));