0

在我的项目中,我正在尝试读取一个 excel 文件。然而,奇怪的事情发生了。当我明显地打开 excel 时,它会正确执行。当我将它设置为不可见时,它不会打开我的文件。

Qt版本:qt-opensource-windows-x86-msvc2015_64-5.7.0

Windows 版本:64 位 win-10

控制台中的错误信息:

QAxBase:调用 IDispatch 成员打开时出错:未知错误

读取Excel文件的代码:

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;
4

3 回答 3

0

我遇到了同样的情况,当它不可见时,我试试这个:

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

有用。

qt 版本:4.8.6 窗口版本:10 excel:2013

于 2018-06-27T03:02:18.183 回答
0

尝试这个:

workbooks->querySubObject("Open(const QString&)",QString(path));
于 2017-03-06T09:44:22.140 回答
-1

知道了 !我认为这是 Excel 服务错误。

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;

我不知道原因,但它有点飞溅。希望它可以帮助你。

于 2017-08-14T05:24:04.903 回答