2016-11-16 92 views
0

在我的項目中,我試圖讀取一個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; 

回答

0

試試這個:

workbooks->querySubObject("Open(const QString&)",QString(path)); 
+0

雖然這段代碼片段是受歡迎的,並且可能會提供一些幫助,但如果它包含* how *和* why *的解釋(// meta.stackexchange.com/q/114762)解決了這個問題。請記住,你正在爲將來的讀者回答這個問題,而不僅僅是現在問的人!請編輯您的答案以添加解釋,並指出適用的限制和假設。 –

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

我不是原因,但它有點飛濺。希望它可以幫助你。