您的位置:首页 > 移动开发 > Objective-C

Using ActiveX Object in Qt

2015-12-24 22:42 651 查看
QAxObject* excel = new QAxObject( "Excel.Application", 0 );
QAxObject* workbooks = excel->querySubObject( "Workbooks" );
QAxObject* workbook = workbooks->querySubObject( "Open(const QString&)", "C:\\1.xls" );
QAxObject* sheets = workbook->querySubObject( "Worksheets" );
QList<QVariantList> data; //Data list from excel, each QVariantList is worksheet row

//worksheets count
int count = sheets->dynamicCall("Count()").toInt();

count = sheets->property("Count").toInt();
for (int i=1; i<=count; i++) //cycle through sheets
{
//sheet pointer
QAxObject* sheet = sheets->querySubObject( "Item( int )", i );

QAxObject* rows = sheet->querySubObject( "Rows" );
int rowCount = rows->dynamicCall( "Count()" ).toInt(); //unfortunately, always returns 255, so you have to check somehow validity of cell values
QAxObject* columns = sheet->querySubObject( "Columns" );
int columnCount = columns->property("Count").toInt();

for (int row=1; row <= rowCount; row++)
{
QVariantList dataRow;
bool isEmpty = true; //when all the cells of row are empty, it means that file is at end (of course, it maybe not right for different excel files. it's just criteria to calculate somehow row count for my file)
for (int column=1; column <= columnCount; column++)
{
//Do something usefule here
}
if (isEmpty) //criteria to get out of cycle
break;
data.append(dataRow);

}
}

workbook->dynamicCall("Close()");
excel->dynamicCall("Quit()");

QFile file1("c://sheet2.html");
file1.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file1);
out << excel->generateDocumentation();
file1.close();


sheet->querySubObject( "Pictures" )


转自 https://wiki.qt.io/Using_ActiveX_Object_in_Qt
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: