前面主要介绍了Windows控件开发及Office开发相关的知识点,在这篇文章中我们正式来介绍一下如何结合前面的知识点来构建Office控件。
开发内嵌在Windows应用程序的Office控件一般有以下三种解决方案:
1、通过API把Office的窗口句柄给Windows应用程序;
2、通过WebBrowser控件;
3、利用DsoFramer控件。
通过了解网上了解资料及一定的测试,最后还是决定用第一种方案来实现。
范围 |
描述 |
控件文件 |
对Window应用程序提供属性,方法及事件等 |
Office接口 |
Office相关功能的具体定义 |
Office实现 |
针对定义接口各种Office不同版本之间的实现 |
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;
主要介绍Excel2007的打开实现功能
OfficeEmbedCtl.cs中打开Office文档实现代码如下:
public partial class 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 interface IOfficeEmbed
{
///<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;
}
}
}