QT加载office文件简单方法
2016-02-24 16:51
579 查看
记录下:
QT 使用COM组件加载office的文件,使用需要安装MS office。WORD,EXCEL文件可以直接嵌入;PPT文件无法直接嵌入。能嵌入的同志望回复告知。下面描述了 加载;*.doc;*.docx;*.docm;*.xls;*.xlsx;*.xlsm;*.xlsb,*.ppt;*.pptx;*.pptm;*.txt;*.xml;这几种文件。源代码如下:
一:组件皮肤文件 utofficewidget.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>UTOfficeWidget</class>
<widget class="QWidget" name="UTOfficeWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>UTOfficeWidget</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTextEdit" name="textEdit"/>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
二:头文件 utofficewidget.h
#ifndef UTOFFICEWIDGET_H
#define UTOFFICEWIDGET_H
#include <QWidget>
#include<QAxObject>
#include<QAxWidget>
#include <QTextStream>
#include<QFile>
#include "ui_utofficewidget.h"
class UTOfficeWidget : public QWidget
{
Q_OBJECT
public:
UTOfficeWidget(QWidget *parent = 0);
~UTOfficeWidget();
public:
//打开文件 ;*.doc;*.docx;*.docm;*.xls;*.xlsx;*.xlsm;*.xlsb,*.ppt;*.pptx;*.pptm;*.txt;*.xml;
void Open(QString filePath);
//关闭 是否需要保存 默认不保存
void Close(bool isSave=false);
//保存
void Save();
//另存为。。
void SaveAs(QString newFilePath);
private:
//文档打开控件
QAxWidget* officeContent;
//当前打开文件的路径
QString currentFilePath;
Ui::UTOfficeWidget ui;
};
#endif // UTOFFICEWIDGET_H
三:CPP文件:
#include "utofficewidget.h"
UTOfficeWidget::UTOfficeWidget(QWidget *parent)
: QWidget(parent)
,officeContent(NULL)
{
ui.setupUi(this);
}
UTOfficeWidget::~UTOfficeWidget()
{
Close();
}
void UTOfficeWidget::Open( QString filePath )
{
if (filePath.isEmpty())
{
return;
}
if (officeContent)
{
Close();
}
if (filePath.endsWith(".docx") ||filePath.endsWith(".doc") || filePath.endsWith(".doc"))
{
officeContent=new QAxWidget ("Word.Document", ui.textEdit);
officeContent-> setGeometry (QRect (10, 10, this->width()-10, this->height()-10));
officeContent->setControl (filePath);
officeContent-> show ();
}
else if (filePath.endsWith(".xlsx") || filePath.endsWith(".xls") ||filePath.endsWith(".xlsm"))
{
officeContent=new QAxWidget ("Excel.WorkBook", ui.textEdit);
officeContent-> setGeometry (QRect (10, 10, this->width()-10, this->height()-10));
officeContent->setControl (filePath);
officeContent-> show ();
}
else if (filePath.endsWith(".ppt") || filePath.endsWith(".pptx") ||filePath.endsWith(".pptm"))
{
//"Powerpoint.Presentation"
officeContent=new QAxWidget ("Powerpoint.Application",ui.textEdit);
//pptPresentation->setControl("{048EB43E-2059-422F-95E0-557DA96038AF}");
//pptPresentation->setControl("{91493441-5A91-11CF-8700-00AA0060263B}");
officeContent-> setGeometry (QRect (10, 10, this->width()-10, this->height()-10));
QAxObject *presentations = officeContent->querySubObject( "Presentations" );
presentations->dynamicCall("Open(QString)",filePath);
}
else if (filePath.endsWith(".txt") || filePath.endsWith(".xml"))
{
QFile sfile(filePath);
if (sfile.open(QFile::ReadOnly | QFile::Text))
{
QTextStream in(&sfile);
ui.textEdit->setText(in.readAll());
sfile.close();
}
}
else
{
return;
}
currentFilePath=filePath;
}
void UTOfficeWidget::Close( bool isSave/*=false*/ )
{
if (officeContent)
{
if (currentFilePath.endsWith(".ppt") || currentFilePath.endsWith(".pptx") ||currentFilePath.endsWith(".pptm"))
{
officeContent->dynamicCall("Quit(void)");
}
else if (currentFilePath.endsWith(".txt") || currentFilePath.endsWith(".xml"))
{
ui.textEdit->clear();
}
else
{
officeContent->dynamicCall("Close(bool)",true);
}
}
}
void UTOfficeWidget::Save()
{
}
void UTOfficeWidget::SaveAs( QString newFilePath )
{
}
四:调用测试:
UTOfficeWidget *officeConent=new UTOfficeWidget(this);
officeConent->setFixedSize(this->width(),this->height());
officeConent->Open(QString("D:/123.docx"));
ui.horizontalLayout->addWidget(officeConent);
相关文章推荐
- QT自定义精美换肤界面
- Qt获取当前时间并格式化输出及将积秒转换成时间
- QT QVector<QPair<QString, qint64>> qSort 排序
- 一起看代码来玩玩QT之10 Graphics
- qt自定义搜索框(超简单,带效果图)
- mp4格式文件转码后处理(qt-faststart).md
- qt5-qmake
- PyCharm+QT Designer整合
- Qt构建工具QBS之一 ——从一个简单的工程开始
- 《转》PyQt之自定义无边框窗口遮盖任务栏显示问题
- Qt构建工具QBS之零 —— QBS 概览
- QT5 MSVC2013数据库SQLserver操作中文显示乱码问题
- QT Creator 两种创建项目的方法
- Qt浅谈之窗体缩放(仅增加测试代码)
- Qt上截取中英文混合字符串的指定长度内容
- 用Qt实现一个桌面弹幕程序(六)-- -- 桌面客户端实现②
- opencv + Qt5.5 + cmake(你遇到的问题,我知道)
- qt的tabwidget在button点击下,切换页面
- PyQt4入门教程(5)_事件与信号
- Qt对话框QDialog的返回值