如何在的Qt项目中嵌入word,excel,ppt窗口

2025-11-19 23:12:11

1、在打开的VS2013中新建一个项目,如下图所示,选择Qt GUI Application,并输入创建的文件名和文件位置,点击确定,Next即可,我输入的文件名为QtOffice;

如何在的Qt项目中嵌入word,excel,ppt窗口

2、创建好项目之后,点击创建的ui文件进行窗口设置,为方便教程,在窗口中只加入一个打开按钮和显示word、ppt、excel的窗口,如下图所示;

如何在的Qt项目中嵌入word,excel,ppt窗口

3、注意:需要在右键属性中的链接器下的附加依赖项中添加

Qt5AxContainerd.lib

Qt5AxBased.lib

不然会编译不通过!!!

如何在的Qt项目中嵌入word,excel,ppt窗口

4、在QtOffice.h中添加如下代码:

public slots:

void openOffice();

public:

void OpenWord(QString& str);

void OpenExcel(QString& str);

void openPpt(QString& str);

public:

QAxWidget* _excelview;

5、和QtOffice.cpp中添加如下代码:

在构造函数中添加

_excelview = NULL;

connect(ui.btnOpen, SIGNAL(clicked()), this, SLOT(openOffice()));

打开excel的方法的代码:

void QtOffice::OpenExcel(QString& fileName)

{

_excelview = new QAxWidget("Excel.Application", 0);

_excelview->dynamicCall("SetVisible (bool Visible)", "false");

_excelview->setProperty("DisplayAlerts", false);

auto rect = ui.widget->geometry();

_excelview->setGeometry(rect);

_excelview->setControl(fileName);

_excelview->show();

}

打开dialog的方法的代码:

void QtOffice::openOffice()

{

QFileDialog _dialog;

_dialog.setFileMode(QFileDialog::ExistingFile);

_dialog.setViewMode(QFileDialog::Detail);

_dialog.setOption(QFileDialog::ReadOnly, true);

_dialog.setDirectory(QString("./"));

_dialog.setNameFilter(QString("所有文件(*.*);;word(*.docx *.doc);;excel(*.xlsx);;ppt(*.pptx *.ppt)"));

if (_dialog.exec())

{

QStringList files = _dialog.selectedFiles();

for (auto fileName : files)

{

if (fileName.endsWith(".xlsx"))

{

this->OpenExcel(fileName);

}

else if (fileName.endsWith(".docx"))

{

this->OpenWord(fileName);

}

else if (fileName.endsWith(".pptx"))

{

this->openPpt(fileName);

}

}

}

}

6、点击本地调试即可,在窗口中点击打开选择需要打开的文件,实现如下图所示,

打开的是excel文件,word和ppt同理。

如何在的Qt项目中嵌入word,excel,ppt窗口

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢