在我的项目中,我正在尝试读取一个 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;