轉(zhuǎn)帖|其它|編輯:郝浩|2011-10-13 15:45:15.000|閱讀 613 次
概述:前面主要介紹了Windows控件開發(fā)及Office開發(fā)相關(guān)的知識(shí)點(diǎn),在這篇文章中我們正式來介紹一下如何結(jié)合前面的知識(shí)點(diǎn)來構(gòu)建Office控件。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
前面主要介紹了Windows控件開發(fā)及Office開發(fā)相關(guān)的知識(shí)點(diǎn),在這篇文章中我們正式來介紹一下如何結(jié)合前面的知識(shí)點(diǎn)來構(gòu)建Office控件。
1.1. 技術(shù)選型
開發(fā)內(nèi)嵌在Windows應(yīng)用程序的Office控件一般有以下三種解決方案:
1、通過API把Office的窗口句柄給Windows應(yīng)用程序;
2、通過WebBrowser控件;
3、利用DsoFramer控件。
通過了解網(wǎng)上了解資料及一定的測(cè)試,最后還是決定用第一種方案來實(shí)現(xiàn)。
1.2. 結(jié)構(gòu)設(shè)計(jì)
范圍 | 描述 |
控件文件 | 對(duì)Window應(yīng)用程序提供屬性,方法及事件等 |
Office接口 | Office相關(guān)功能的具體定義 |
Office實(shí)現(xiàn) | 針對(duì)定義接口各種Office不同版本之間的實(shí)現(xiàn) |
1.3. 功能接口設(shè)計(jì)
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文檔標(biāo)簽內(nèi)容FillCell (DataTable tbl)
1.6. 關(guān)閉Office文檔void Close()
1.7. 退出Office程序void Quit()
2. 控件屬性
2.1. 激活Office應(yīng)用程序object ActiveApplication
2.2. 激活Office工作文檔object ActiveDocument
2.3. Office文檔的名稱 string DocumentFullName
3. 事件
3.1. 文檔關(guān)閉前發(fā)生event EventHandler BeforeClose;
3.2. 點(diǎn)擊重新填充按鈕時(shí)發(fā)生event EventHandler BeforeClose;
3.3. 點(diǎn)擊插入詞庫右鍵菜單時(shí)發(fā)生event EventHandler BeforeClose;
1.4. 軟件代碼實(shí)現(xiàn)
主要介紹Excel2007的打開實(shí)現(xiàn)功能
OfficeEmbedCtl.cs中打開Office文檔實(shí)現(xiàn)代碼如下:
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文檔實(shí)現(xiàn)代碼如下:
/// <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文檔實(shí)現(xiàn)代碼如下:
/// <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;
}
}
}
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:網(wǎng)絡(luò)轉(zhuǎn)載