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

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

3、注意:需要在右键属性中的链接器下的附加依赖项中添加
Qt5AxContainerd.lib
Qt5AxBased.lib
,不然会编译不通过!!!

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同理。
