qt操作excel報錯解決

如果電腦上沒有office,需要判斷,否则,会报错

onecore\com\combase\catalog\catalog.cxx(2376)\combase.dll!00007FFF1DF823CB: (caller: 00007FFF1DED3A10) ReturnHr(7) tid(3a5c) 800401F3 无效的类字符串
线程 0x3b74 已退出,返回值为 0 (0x0)。
onecore\com\combase\dcomrem\resolver.cxx(2279)\combase.dll!00007FFF1DE596F0: (caller: 00007FFF1DECE463) ReturnHr(8) tid(3a5c) 80040154 没有注册类
onecore\com\combase\dcomrem\resolver.cxx(2483)\combase.dll!00007FFF1DECE48A: (caller: 00007FFF1DE58779) ReturnHr(9) tid(3a5c) 80040154 没有注册类
CoCreateInstance failure (没有注册类)
QAxBase::setControl: requested control Excel.Application could not be instantiated
QAxBase::dynamicCallHelper: Object is not initialized, or initialization failed
QAxBase::dynamicCallHelper: Object is not initialized, or initialization failed
0x00007FF621D0DD8E 处(位于 SpinalV1i.exe 中)引发的异常: 0xC0000005: 读取位置 0x0000000000000018 时发生访问冲突。

解决代码如下:

//根据文件名获取文件数据
QAxObject* getExcelDataByFilename(QString fileNameParam)
{
    QAxObject *worksheets;
    QAxObject *workbook;
    try 
    {
        QAxObject *excel = new QAxObject(this);    //连接Excel控件
        if (excel->setControl("Excel.Application"))
        {

        }
        else
        {
            excel->setControl("ket.Application");  //连接Excel控件
        }
        excel->setProperty("Visible", false);  //不显示窗体
        QAxObject *workbooks = excel->querySubObject("WorkBooks");
        workbook = workbooks->querySubObject("Open(const QString&)", fileNameParam);
    }
    catch (...) {
        qCritical() << "打开文件失败...";
        
    }

    return workbook;
}

 

posted @ 2019-10-24 22:19  西北逍遥  阅读(2964)  评论(0编辑  收藏  举报