Excel with Qt: find number last filled row

我只是一个虾纸丫 提交于 2019-12-12 03:16:28

问题


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:


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


来源:https://stackoverflow.com/questions/27108153/excel-with-qt-find-number-last-filled-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!