Help to solve an xlsx file with QAxObject
-
Hey guys!
I have been looking for a way to open xlsx files and I found the solution on this very forum. I actually manage to open an xlsx file and to read it with this code :
includes : qaxobject, qaxwidget, qapplication, ActiveQt/QAxBase, ActiveQt/QAxObject
.pro: QT+=axcontainer, CONFIG += axcontainer
@
QAxObject* excel = new QAxObject("Excel.Application");
excel->setProperty("Visible", false);QAxObject* workbooks = excel->querySubObject("WorkBooks"); workbooks->dynamicCall("Open (const QString&)", QString("D:\\BA.xlsx")); //filename QAxObject* workbook = excel->querySubObject("ActiveWorkBook"); QAxObject* worksheets = workbook->querySubObject("WorkSheets"); QAxObject* worksheet = workbook->querySubObject("Worksheets(int)", 1); //worksheet number QAxObject* usedrange = worksheet->querySubObject("UsedRange"); QAxObject* rows = usedrange->querySubObject("Rows"); QAxObject* columns = usedrange->querySubObject("Columns"); int intRowStart = usedrange->property("Row").toInt(); int intColStart = usedrange->property("Column").toInt(); int intCols = columns->property("Count").toInt(); int intRows = rows->property("Count").toInt(); QAxObject * cell; // QString s; // for (int i = intRowStart; i < intRowStart + intRows; i++) // { // for (int j = intColStart; j < intColStart + intCols; j++) // { // cell = excel->querySubObject("Cells(Int, Int)", i, j ); // QVariant cellValue = cell->dynamicCall("value"); // s += QString::number(i)+ // " "+QString::number(j)+ // " "+ // cellValue.toString()+"\n" ; //value of the cell // } // } // qDebug() << s; cell = excel->querySubObject("Cells(Int, Int)", 11, 2 ); QVariant cellValue2 = cell->dynamicCall("value"); qDebug() << cellValue2.toString(); cellValue2.setValue(10) ; //value of the cell qDebug() << cellValue2.toString(); workbook->dynamicCall("SaveAs (const QString&)", QString("D:\\test.xlsx")); excel->setProperty("DisplayAlerts", 0); workbook->dynamicCall("Close (Boolean)", false); excel->setProperty("DisplayAlerts",1); excel->dynamicCall("Quit()"); delete excel; return; @
If you try this code, it should work for opening and reading an xlsx file.
Moreover, with this, I manage to modify the cells, and it seems (qDebug) working. However, when i try to save the modified worksheet, it creates the exactly same .xlsx than the one I opened at the begining of my code.
Do you have a clue of what I could try?In addition, I don't know why, but when I execute this code, it never ends, and I have to manually interrupt the process, any idea?
Thank you very much guys!