我在Qt中打开了excel表,并尝试使用以下代码填充行数和列数(如果所有填充连续):
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));
}
问题是rowcount和columnCount显示了一个很大的数字,但我的工作表填充了4x6。
我见过这些问题:Get Last non Empty Cell of Excel Column Programatically
Finding the last filled row in Excel sheet in c#
但两者都是针对C#
但我不知道如何在Qt中这样做
我用Excel.Range和UsedRange解决了这个问题(在这种情况下,在空单元格之间不需要连续填充所有单元格)
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));