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
Using ActiveX Object in Qt
最新推荐文章于 2024-01-31 15:37:59 发布