In my project, I'm trying to read a excel file. However, strange things happened. When I open excel visibly, it will execute correctly. While when I set it invisible, it will not open my file.
Qt Version: qt-opensource-windows-x86-msvc2015_64-5.7.0
Windows Version: 64-bit win-10
Error information in Console:
QAxBase: Error calling IDispatch member Open: Unknown error
The code to read Excel file:
QAxObject *excel = NULL;
QAxObject *workbooks = NULL;
QAxObject *workbook = NULL;
excel = new QAxObject("Excel.Application");
excel->dynamicCall("SetVisible(bool)", false);
// The code to set invisible, project will work correctly when set visible true
workbooks = excel->querySubObject("WorkBooks");
if(!workbooks){
QMessageBox msgBox;
msgBox.setWindowTitle("error information");
msgBox.setText("workbooks error");
msgBox.exec();
return;
}
workbook = workbooks->querySubObject("Open(const QString&, QVariant)", file->filePath, 0);
//This code will not execute correctly, causing "workbook error"
if(!workbook){
QMessageBox msgBox;
msgBox.setWindowTitle("error information");
msgBox.setText("workbook error");
msgBox.exec();
return;
}
QAxObject * worksheet = workbook->querySubObject("WorkSheets(int)", 1);
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();
workbook->dynamicCall("Close (Boolean)", false);
delete excel;