C#:使用dsoframer.ocx控件实现内嵌office效果(详解)

1. 问题描述

最近在研究“如何实现在桌面程序中,实现内嵌office的功能,能够对办公文件(Excel/ Word/ PPT/ Visio)实现常见的查看、编辑、保存等功能”的问题。在思考实现的技术思路时,接触到了关联的ActiveX控件:dsoframer.ocx。于是,看了许多文章,趟过了一些坑,此处总结了一个简单的demo。希望能给需要的人以参考。

2. 效果展示

实现功能:

注册dsoframer.ocx控件。
取消注册dsoframer.ocx控件。
加载 ppt / excel / word 等office常用文件类型。
使用同一个AxDSOFramer.AxFramerControl类对象,在不同文件类型的文件间进行切换。
图片如下:

 

 

 

 

3. 步骤描述

注意: 需要本地安装了Office,dsoframer.ocx控件实质上是控制本地安装好的office来编辑文件,是一种在线编辑的形态。

不仅仅可以应用在桌面程序上,web网页上调用ActiveX控件也能达到同样的效果。
如果由需要,可以自行研究。

具体步骤:(总共就3个步骤)

  1. 注册“dsoframer.ocx”。
  2. 打开vs2017,项目中添加Com组件。
  3. 查看API文件,编写代码调用相关接口,实现所需功能。

尤其注意:

在vs中添加COM组件时,如果没有注册dsoframer.ocx,无法正常将其添加到工具箱,从而无法正常使用。

添加过程如下:

1.在工具箱中,点击右键菜单,弹出项中点击“选择项”。

 2.在弹出窗口中,选择“Com组件” 选择框,勾选“DSO Framer Control Object”,点击确定。

注意:一定要注册dsoframer.ocx后,才能在添加com组件时,找到"DSO Framer Control Object"。

3.然后在工具箱中,成功添加了“DSO Framer Control Object”控件,可像按钮一样拖拽使用。

4. 关键步骤代码

1.注册dsoframer.ocx的类:

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test.MSODsoFramer
{
    public class Register
    {
        /// <summary>
        /// 注册dsoframer.ocx
        /// </summary>
        /// <returns>返回-1:注册失败;返回1:注册成功;返回2:已注册过,不用重复注册</returns>
        public static int RegisterDsoFramer()
        {
            String dsoframerFileName = "DSOFramer2.3.ocx";
            // 存放ocx文件的目录: bin\Debug\Resources\dsoframer.ocx
            string fileFullPath = string.Format("{0}\\Resources\\{1}", System.Environment.CurrentDirectory, dsoframerFileName);
            string systemDrive = System.Environment.GetEnvironmentVariable("systemdrive");//获取系统所在的盘符
            // 确保dsoframer.ocx存在
            if (!File.Exists(fileFullPath) || String.IsNullOrEmpty(systemDrive))
                return -1; // dsoframer.ocx不存在,注册失败
            bool isRegisted = IsRegistered("00460182-9E5E-11D5-B7C8-B8269041DD57");
            if (isRegisted)
                return 2; // 已注册过,注册成功

            string windowsPath = string.Empty;
            if (Win.Distinguish64or32System() == "64")
                windowsPath = string.Format("{0}\\Windows\\SysWOW64", systemDrive);
            else
                windowsPath = string.Format("{0}\\Windows\\System32", systemDrive);
            if (!Directory.Exists(windowsPath))
                return -1;//目标目录不存在,注册失败
            // 复制dsoframer.ocx文件到C:\\Windows\\SysWOW64 或 C:\\Windows\\System32
            File.Copy(fileFullPath, string.Format("{0}\\{1}", windowsPath, dsoframerFileName), true);

            bool result = Registe(string.Format("{0}\\{1}", windowsPath, dsoframerFileName)); //开始注册

            if (result)
            {
                return -1; //注册失败
            }
            return 1;//注册成功
        }

        /// <summary>
        /// 注册dsoframer.ocx  
        /// </summary>
        /// <param name="fileFullName">dsoframer.ocx文件路径</param>
        /// <returns>注册结果</returns>
        private static bool Registe(string fileFullName)
        {
            bool result = false;
            System.Diagnostics.Process p = System.Diagnostics.Process.Start("regsvr32", fileFullName + " /s");//注册完毕不显示是否成功的提示  
            //System.Diagnostics.Process p = System.Diagnostics.Process.Start("regsvr32", fileFullName);//注册完毕显示是否成功的提示  
            if (p != null && p.HasExited)
            {
                Int32 exitCode = p.ExitCode;
                if (exitCode == 0)
                    result = true;
            }
            return result;//成功时,返回false
        }
        /// <summary>
        /// 取消注册dsoframer.ocx
        /// </summary>
        /// <param name="fileFullName">dsoframer.ocx文件路径</param>
        /// <returns></returns>
        private static bool UnRegiste(string fileFullName)
        {
            bool result = false;
            System.Diagnostics.Process p = System.Diagnostics.Process.Start("regsvr32", fileFullName + " /u");//取消注册
            if (p != null && p.HasExited)
            {
                Int32 exitCode = p.ExitCode;
                if (exitCode == 0)
                    result = true;
            }
            return result;//成功时,返回false
        }

        /// <summary>
        /// 判断dsoframer.ocx控件是否已经注册(CLSID='"00460182-9E5E-11D5-B7C8-B8269041DD57"')
        /// </summary>
        /// <param name="CLSID"></param>
        /// <returns></returns>
        private static bool IsRegistered(String CLSID)
        {
            if (String.IsNullOrEmpty(CLSID))
                return false;

            String key = String.Format(@"CLSID\{{{0}}}", CLSID);
            RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(key);
            if (regKey != null)
                return true;
            else
                return false;
        }

    }
}

 2.使用举例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test.MSODsoFramer
{
    public class MicroDsoFramer
    {
        private AxDSOFramer.AxFramerControl m_axFramerControl;

        public MicroDsoFramer(AxDSOFramer.AxFramerControl _m_axFramerControl)
        {
            m_axFramerControl = _m_axFramerControl;
            //隐藏AxDSOFramer.AxFramerControl对象的“标题栏、菜单栏、工具栏”,对应隐藏office的“标题栏、菜单栏、工具栏”。
            m_axFramerControl.Titlebar = false;
            m_axFramerControl.Menubar = false;
            m_axFramerControl.Toolbars = false;
        }

        

        /// <summary>
        /// 初始化office控件,加载Excel/Word/PPT
        /// </summary>
        /// <param name="_sFilePath"></param>
        public void InitOfficeControl(string _sFilePath)
        {
            try
            {
                if (m_axFramerControl == null)
                {
                    throw new ApplicationException("请先初始化office控件对象!");
                }

                //this.m_axFramerControl.SetMenuDisplay(48);
                //这个方法很特别,一个组合菜单控制方法,我还没有找到参数的规律,有兴趣的朋友可以研究一下
                string sExt = System.IO.Path.GetExtension(_sFilePath).Replace(".", "");
                //this.m_axFramerControl.CreateNew(this.LoadOpenFileType(sExt));//创建新的文件
                this.m_axFramerControl.Open(_sFilePath, false, this.LoadOpenFileType(sExt), "", "");//打开文件
                                                                                                    //隐藏标题
                this.m_axFramerControl.Titlebar = false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //下面这个方法是dso打开文件时需要的一个参数,代表office文件类型
        /// <summary>
        /// 根据后缀名得到打开方式
        /// </summary>
        /// <param name="_sExten"></param>
        /// <returns></returns>
        private string LoadOpenFileType(string _sExten)
        {
            try
            {
                string sOpenType = "";
                switch (_sExten.ToLower())
                {
                    case "xls":
                        sOpenType = "Excel.Sheet";
                        break;
                    case "xlsx":
                        sOpenType = "Excel.Sheet";
                        break;
                    case "doc":
                        sOpenType = "Word.Document";
                        break;
                    case "docx":
                        sOpenType = "Word.Document";
                        break;
                    case "ppt":
                        sOpenType = "PowerPoint.Show";
                        break;
                    case "pptx":
                        sOpenType = "PowerPoint.Show";
                        break;
                    case "vsd":
                        sOpenType = "Visio.Drawing";
                        break;
                    default:
                        sOpenType = "Word.Document";
                        break;
                }
                return sOpenType;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}

5. 总结

此次demo实现功能点不多,但可以帮助入门借助dsoframer.ocx控件实现在线编辑office功能的研究,能基本完成在winform桌面程序中,内嵌office办公程序的效果。

关于dsoframer.ocx的注册和使用,可以参考几篇百度经验文章:
1.注册:https://jingyan.baidu.com/article/3aed632e78ea893111809115.html
2.取消注册:https://jingyan.baidu.com/article/b7001fe177fb964f7382dd15.html
3.在vs中使用:https://jingyan.baidu.com/article/2fb0ba4052cf5b41f3ec5f15.html

转自:C#:使用dsoframer.ocx控件实现内嵌office效果(详解)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值