是的,可以使用QAxObject在已有的Excel文件中插入一张图片。下面是一个示例代码:

#include <QApplication>
#include <QAxObject>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QAxObject excel("Excel.Application");
    excel.setProperty("Visible", true);
    QAxObject *workbooks = excel.querySubObject("Workbooks");
    QAxObject *workbook = workbooks->querySubObject("Open(const QString&)", "path_to_your_excel_file");

    QAxObject *sheets = workbook->querySubObject("Sheets");
    QAxObject *sheet = sheets->querySubObject("Item(int)", 1); // Assuming you want to insert the picture in the first sheet

    QAxObject *pictures = sheet->querySubObject("Pictures");
    QAxObject *picture = pictures->querySubObject("Insert(const QString&)", "path_to_your_image_file");
    picture->setProperty("Left", 100); // Set the position of the picture
    picture->setProperty("Top", 100);

    workbook->dynamicCall("Save()");
    workbook->dynamicCall("Close()");

    excel.dynamicCall("Quit()");

    delete picture;
    delete pictures;
    delete sheet;
    delete sheets;
    delete workbook;
    delete workbooks;

    return a.exec();
}

请将path_to_your_excel_file替换为您的Excel文件的路径,并将path_to_your_image_file替换为您要插入的图片的路径。在示例中,我们假设要将图片插入到第一个工作表中,并将其位置设置为左上角的(100,100)。最后,保存并关闭工作簿,并退出Excel应用程序。

请注意,为了使用QAxObject类,您需要将QT += axcontainer添加到您的.pro文件中,并且您的系统上必须安装有Microsoft Office

qt c++能使用QAxObject在已有excel文件中插入一张图片

原文地址: https://www.cveoy.top/t/topic/hJii 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录