轉帖|其它|編輯:郝浩|2011-10-13 15:45:15.000|閱讀 613 次
概述:前面主要介紹了Windows控件開發及Office開發相關的知識點,在這篇文章中我們正式來介紹一下如何結合前面的知識點來構建Office控件。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
前面主要介紹了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 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;
}
}
}
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:網絡轉載