在使用QAxWidget进行EXCEL保存时提示错误:
QAxBase: Error calling IDispatch member SaveAs: Exception thrown by server
Code : -2146827284
Source : Microsoft Excel
Description: Microsoft Excel ????????????D:\//StudyTemp/QtProject/MultilingualTools/build-MultilingualTools-Desktop_Qt_6_2_4_MinGW_64_bit-Debug
Help : xlmain11.chm
Connect to the exception(int,QString,QString,QString) signal to catch this exception
报错原因:保存路径为程序路径的相对路径,获取的路径格式为:D:/APP/xxx/xx ,QAxWidget无法识别路径。
//获取当前程序运行路径
QString CurrentPath = QCoreApplication::applicationDirPath() ;
// 保存Excel文件
workbook->dynamicCall("SaveAs(const QString&)",CurrentPath );
解决方法:将保存路径该为程序路径的绝对路径,使用文本替换,将路径格式替换为:D:\\APP\\xxx\\xx,程序修改如下。
//获取当前程序运行路径
QString CurrentPath = QCoreApplication::applicationDirPath() ;
//将路转换为绝对路径
QString AbsolutePath = CurrentPath.replace("/", "\\");
// 保存Excel文件
workbook->dynamicCall("SaveAs(const QString&)",AbsolutePath );