问题出现的原因:
workbook->dynamicCall(“SaveAs (const QString &)”, file);file是不能用’/’,
解决方法:把’/‘替换成’\\’
如果是调用QCoreApplication::applicationDirPath()等QT的API获取路径的,可以使用replace(’/’, ‘\\’)把’/‘替换成’\\’。
相关代码:
// 创建 Excel 文件
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setDefaultSuffix("xlsx");
QString filePath = dialog.getSaveFileName(this, "Save Excel File", QDir::homePath(), "Excel Files (*.xlsx)");
QString filePath1 = filePath.replace('/','\\');
// 保存 Excel 文件
workbook->dynamicCall("SaveAs(const QString&)", filePath1);
workbook->dynamicCall("Close()");
excel->dynamicCall("Quit()");
delete excel;