在我的项目中,我试图读取一个excel文件。然而,奇怪的事情发生了。当我打开excel可见时,它会正确执行。当我将它设置为不可见时,它不会打开我的文件。奇怪的阅读excel错误与Qt
的Qt版本:QT-开源窗口86 msvc2015_64-5.7.0
Windows版本:64位赢得-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;
虽然这段代码片段是受欢迎的,并且可能会提供一些帮助,但如果它包含* how *和* why *的解释(// meta.stackexchange.com/q/114762)解决了这个问题。请记住,你正在为将来的读者回答这个问题,而不仅仅是现在问的人!请编辑您的答案以添加解释,并指出适用的限制和假设。 –