1

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;
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
P.N. Lee
  • 31
  • 4

3 Answers3

0

try this:

workbooks->querySubObject("Open(const QString&)",QString(path));
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    Whilst this code snippet is welcome, and may provide some help, it would be [greatly improved if it included an explanation](//meta.stackexchange.com/q/114762) of *how* and *why* this solves the problem. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Mar 06 '17 at 11:31
0

i meet same situation,and when it is invisible,i try this:

excel->setProperty("EnableEvents",false);

it works.

qt-version:4.8.6 window-version:10 excel:2013

green new
  • 11
  • 2
-1

Got it ! I think this is Excel Service bug.

Just change your code :
excel->dynamicCall("SetVisible(bool)", **true**); 
And add another: 
excel->dynamicCall("SetVisible(bool)", **false**),
after workbooks = excel->querySubObject("WorkBooks");
work on Qt5.6 excel 2013;

I do not the reason,but it works with a little splash. Hope it could help you.

Pravitha V
  • 3,308
  • 4
  • 33
  • 51
hugo
  • 1