Office控件开发总结-构建Office控件
2013-05-07 09:57
471 查看
在写技术研究报告时,看到的一篇不错的文档,摘抄过来方便以后查阅
前面主要介绍了Windows控件开发及Office开发相关的知识点,在这篇文章中我们正式来介绍一下如何结合前面的知识点来构建Office控件。
1、通过API把Office的窗口句柄给Windows应用程序;
2、通过WebBrowser控件;
3、利用DsoFramer控件。
通过了解网上了解资料及一定的测试,最后还是决定用第一种方案来实现。
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;
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;
}
}
}
前面主要介绍了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;
}
}
}
相关文章推荐
- Office控件开发总结-构建Office控件
- Office控件开发总结-开发Windows 窗体控件简单概述
- 基于MVC+EasyUI的Web开发框架经验总结(2)- 使用EasyUI的树控件构建Web界面
- Office控件开发总结-Office 开发基本知识
- Office控件开发总结-Office 开发基本知识
- 基于MVC4+EasyUI的Web开发框架经验总结(2)- 使用EasyUI的树控件构建Web界面
- 基于MVC4+EasyUI的Web开发框架经验总结(2)- 使用EasyUI的树控件构建Web界面
- Office控件开发总结-目录
- (转)基于MVC4+EasyUI的Web开发框架经验总结(2)- 使用EasyUI的树控件构建Web界面
- Office控件开发总结-开发Windows 窗体控件简单概述
- Office控件开发总结-Office 编程中的常见任务
- Android TV开发总结(七)构建一个TV app中的剧集列表控件
- Office控件开发总结-Office 编程中的常见任务
- Office控件开发总结-目录
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第6章节--在SharePoint2013中开发、集成和构建应用程序 总结
- 【IOS 开发学习总结-OC-49】★★ios开发之UI控件——UIStepper与UIWebView
- WinForm控件开发总结(十二)-----让控件处理导航键
- 【IOS 开发学习总结-OC-51】★★★ios开发之UI控件——UITableView 与UITableViewCell
- [网上搜集]c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- 【转】c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)