记录下:

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加载office文件简单方法相关推荐

  1. vue加载服务器json文件,Vue加载json文件的方法简单示例

    本文实例讲述了Vue加载json文件的方法.分享给大家供大家参考,具体如下: 一.在build/dev-server.js文件里 var app = express() 这句代码后面添加如下(旧版): ...

  2. 异步加载js文件的方法总结

    方法一,jQuery.getScript HTML 代码: 代码如下 复制代码 <button id="go">Run</button> <div c ...

  3. Qt 加载图片文件路径详解

    QT 加载文件,图片路径很容易搞混,需要注意的是WINDOW路径分隔符为"\",QT为"/",我遇到的路径加载总结为三种情况: (1)绝对路径,文件的整个路径, ...

  4. Qt 加载QML 文件的几种方式

    前言 之所以写这篇文章,是因为在项目中经常会碰到一个问题,qml 文件该如何加载到工程中,其实 Qt Quick APP 有两种模式,另外,还有一种场景是,在 QWidget 界面上加载 QML 页面 ...

  5. java如何手撕加载字节码的代码?编写一个加载class文件的方法

    1 自定义ClassLoader类:MemoryClassLoader public class MemoryClassLoader extends URLClassLoader {// class ...

  6. HTML5动态加载资源方式,动态加载JavaScript文件的两种方法

    这篇文章主要为大家详细介绍了动态加载JavaScript文件的两种方法,感兴趣的小伙伴们可以参考一下 第一种便是利用ajax方式,把script文件代码从背景加载到前台,而后对加载到的内容经过eval ...

  7. freecplus框架-加载参数文件

    文章目录 一.源代码说明 二.参数文件的意义 三.CIniFile类 1.类的声明 2.示例程序 四.获取视频教程 五.版权声明 一.源代码说明 freecplus是一个Linux系统下的C/C++开 ...

  8. HTML5动态加载资源方式,动态加载JavaScript文件的3种方式

    以下是遇到的几种动态加载JavaScript文件的方式,持续更新中... 一.使用document.write/writeln()方式 该种方式可以实现js文件的动态加载,原理就是在重写文档流,这种方 ...

  9. Spring如何加载XSD文件(org.xml.sax.SAXParseException: Failed to read schema document错误的解决方法)...

    本文原文连接: http://blog.csdn.net/bluishglc/article/details/7596118 ,转载请注明出处! 有时候你会发现过去一直启动正常的系统,某天启动时会报出 ...

最新文章

  1. Linux火狐解压完运行不了,在Ubuntu系统下firefox账号无法登录的解决
  2. c#分布式ID生成器
  3. 单例模式反射、序列化漏洞及解决方案!
  4. ethernet调试工具_开发者分享 | 如何调试10G/25G以太网IP自协商/Link Training
  5. LeetCode 03. 无重复字符的最长子串
  6. 计算机的网络技术的普及,计算机网络技术的普及与应用-网络技术论文-计算机论文(7页)-原创力文档...
  7. Java 并发编程CountDownLatch的应用与源码解析
  8. ENVI实验教程(7)实验七、遥感影像变化检测
  9. html内容写入txt文件内容,写入内容到文件里面 - FileWriter《 HTML5:文件系统 》
  10. vscode如何调整字体大小
  11. 善用佳软:如何使用Beyond Compare比对class文件
  12. 香港浸会大学计算机学院校友,校友反馈 | 香港浸会大学值不值得去读?
  13. 阵列卡直通模式和raid模式_详解磁盘阵列RAID原理、种类及性能优缺点
  14. 建筑设计的未来是什么?| 建筑 · 人工智能专栏
  15. linux pdf中文乱码,英文乱码(乱码为方格之类的解决方法)
  16. 给要入门量化分析的人一些建议(转)
  17. Visual Studio如何在一个解决方案下创建多个独立项目并单独运行
  18. 【Android】二进制图片和Bitmap的getPixel方法解析
  19. PG使用pg_settings表查看参数的生效条件
  20. new FileReader()

热门文章

  1. 框架-Mybatis
  2. Oracle Solaris 11.4安装桌面/gdb
  3. flask_sqlalchemy迁移数据库出错Context impl MySQLImpl. Will assume non-transactional DDL. No changes in sche
  4. python语句print(chr(65))_Python语句 print(chr(65))的运行结果是
  5. CMS爱好者如何模仿目标站
  6. java毕业生设计医学生在线学习交流平台计算机源码+系统+mysql+调试部署+lw
  7. 2019 前端开发者进阶手册.pdf
  8. Golang工具安装
  9. 图像/视频数据标注工具
  10. 有关批判Arnold的正确姿势的建议