使用Qt的Excel:找到最后填充的行号

时间:2014-11-24 15:21:44

标签: excel qt qt4

我在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

中做到这一点

1 个答案:

答案 0 :(得分:3)

我已经用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));