Office控件开发总结-构建Office控件

本文介绍了一种将Office功能集成到Windows应用程序的方法,重点讲解了使用API实现Office文档操作的技术细节,包括创建、打开、保存文档等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在写技术研究报告时,看到的一篇不错的文档,摘抄过来方便以后查阅

前面主要介绍了Windows控件开发及Office开发相关的知识点,在这篇文章中我们正式来介绍一下如何结合前面的知识点来构建Office控件。

1.1.     技术选型

开发内嵌在Windows应用程序的Office控件一般有以下三种解决方案:

1、通过API把Office的窗口句柄给Windows应用程序;

2、通过WebBrowser控件;

3、利用DsoFramer控件。

通过了解网上了解资料及一定的测试,最后还是决定用第一种方案来实现。

1.2.     结构设计

范围

描述

控件文件

对Window应用程序提供属性,方法及事件等

Office接口

Office相关功能的具体定义

Office实现

针对定义接口各种Office不同版本之间的实现

1.3.     功能接口设计

1.         控件方法

1.1.        新建Office文档void Create(string fileName)

1.2.        打开Office文档void Open(string fileName)

1.3.        保存Office文档void Save()

1.4.        打印Office文档void PrintOut()

1.5.        填充Office文档标签内容FillCell (DataTable tbl)

1.6.        关闭Office文档void Close()

1.7.        退出Office程序void Quit()

2.         控件属性

2.1.        激活Office应用程序object ActiveApplication

2.2.        激活Office工作文档object ActiveDocument

2.3.        Office文档的名称string DocumentFullName

3.         事件

3.1.        文档关闭前发生event EventHandler BeforeClose;

3.2.        点击重新填充按钮时发生event EventHandler BeforeClose;

3.3.        点击插入词库右键菜单时发生event EventHandler BeforeClose;

1.4.     软件代码实现

主要介绍Excel2007的打开实现功能

OfficeEmbedCtl.cs中打开Office文档实现代码如下:

public partialclass OfficeEmbedCtl :UserControl

{

    public OfficeEmbedCtl()

    {

        InitializeComponent();

        _officeEmbeds.Add(".xlsx", new Excel2007Embed());

}

    ///<summary>

    /// 打开文件

    ///</summary>

    ///<param name="fileName">文件名称</param>

    public void Open(string fileName)

    {

        if (!DocumentFullName.Equals(fileName))

        {

            if (_officeEmbed != null)

            {

                _officeEmbed.Close();

            }

            _documentFullName = fileName;

            _officeEmbed = (IOfficeEmbed)_officeEmbeds[DocumentFileExt];

            _officeEmbed.DocumentFullName = DocumentFullName;

            InitOfficeEmbed();

            _officeEmbed.Open(fileName, this.Handle.ToInt32());

        }

}

    private IOfficeEmbed _officeEmbed =null;

    ///<summary>

    /// 初始化Office控件

    ///</summary>

    private void InitOfficeEmbed()

    {

        _officeEmbed.BeforeClose = BeforeClose;

        _officeEmbed.InertLexiconClick = InertLexiconClick;

        this.SizeChanged += delegate

        {

            _officeEmbed.OnResize(this.Handle.ToInt32());

        };

    }

}

IOfficeEmbed.cs中打开Office文档实现代码如下:

///<summary>

/// 提供用于Office操作的功能接口

///</summary>

public interfaceIOfficeEmbed

{

    ///<summary>

    /// 打开文件

    ///</summary>

    ///<param name="fileName">文件名称</param>

    ///<param name="handleId">窗口句柄ID</param>

    void Open(string fileName,int handleId);

}

Excel2007Embed.cs中打开Office文档实现代码如下:

///<summary>

/// 提供用于Excel2007操作的功能

///</summary>

public class Excel2007Embed : IOfficeEmbed

{

    ///<summary>

    /// 打开文件

    ///</summary>

    ///<param name="fileName">文件名称</param>

    ///<param name="handleId">窗口句柄ID</param>

    public void Open(string fileName,int hWndControl)

    {

        try

        {

            if (_xlApp == null)

            {

                InitApplication();

            }

            if (_xlWorkbook != null)

            {

                Close();

            }

            _hWndControl = hWndControl;

            if (_hWndExcel == 0)

            {

                _hWndExcel = Win32API.FindWindow("XLMAIN",null);

                Win32API.SetParent(_hWndExcel, hWndControl);

            }

            object missingValue = Missing.Value;

            if (_xlApp != null && _xlApp.Workbooks != null)

            {

                _xlWorkbook = _xlApp.Workbooks.Open(fileName,

                    missingValue, missingValue, missingValue, missingValue,

                    missingValue, missingValue, missingValue, missingValue,

                    missingValue, missingValue, missingValue,

                    missingValue, missingValue, missingValue);

                _xlWorkbook.BeforeClose += new Excel.WorkbookEvents_BeforeCloseEventHandler(_xlWorkbook_BeforeClose);

                _xlWorkbook.SheetBeforeRightClick += new Excel.WorkbookEvents_SheetBeforeRightClickEventHandler(_xlWorkbook_SheetBeforeRightClick);

                _xlModule = _xlWorkbook.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);

                _xlModule.CodeModule.AddFromString(GetMacroByButtonCustomClick());

            }

            SetExcelStyle(hWndControl);

        }

        catch (Exception ex)

        {

            throw ex;

        }

    }

}

1、Office2000 下内部COM插件的编程实现.................................................................................2 1.1、版权声明..........................................................................................................................2 1.2、内容详情..........................................................................................................................2 2、用VC6.0 编写Word插件..........................................................................................................11 2.1、版权声明........................................................................................................................11 2.2、内容详情........................................................................................................................11 3、探索 Word 2007 开发.............................................................................................................19 3.1、版权声明........................................................................................................................19 3.2、内容详情........................................................................................................................19 3.2.1 我的博客...............................................................................................................19 3.2.2 扩展 Ribbon.........................................................................................................28 3.2.3 管理侧栏...............................................................................................................43 3.2.4 上传图片...............................................................................................................49 3.2.5 部署插件...............................................................................................................56 4、用VC6.0 编写Word插件(Office2007 篇).................................................................................66 4.1、版权声明........................................................................................................................66 4.2、内容详情........................................................................................................................66 5、Microsoft Word 语法高亮插件(v1.2) ................................................................................70 5.1、版权声明........................................................................................................................70 5.2、内容详情........................................................................................................................70 6、VSTO学习笔记........................................................................................................................75 6.1、版权声明........................................................................................................................75 6.2、内容详情........................................................................................................................75 6.2.1 VSTO概述.............................................................................................................75 6.2.2 Excel对象模型.......................................................................................................89 6.2.3 开发Office 2010 64 位COM加载项...................................................................101 6.2.4 从SharePoint 2010 中下载文件.........................................................................117 6.2.5 批量编辑Excel 2010 x64....................................................................................123 6.2.6 在 Excel 2010 中使用RDLC报表.....................................................................131 7、Excel 二次开发系列..............................................................................................................137 7.1、版权声明......................................................................................................................137 7.2、内容详情......................................................................................................................137 7.2.1 Excel 编成模型...................................................................................................137 7.2.2 Excel 常用操作(创建、打开、读取、写入)...............................................139 7.2.3 创建Excel二次开发环境....................................................................................142 7.2.4 操作一个已经存在Excel....................................................................................143 7.2.5 插件开发系列操作.............................................................................................145 7.2.6 引用Excel模板....................................................................................................172 7.2.7 报表服务基础.....................................................................................................174 7.2.8 报表服务实例.....................................................................................................178
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值