資源簡介
使用 WebBrowser 控件,在 C# WINFORM 中嵌入EXCEL, (WORD 同理)。
注:此方法無法使用 OFFICE 菜單,但可以使用工具欄。

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Drawing;
using?System.Data;
using?System.Text;
using?System.Windows.Forms;
using?Office=Microsoft.Office.Core;
using?Microsoft.Office.Interop.Excel;
using?System.Runtime.InteropServices;
using?System.Reflection;
using?System.Runtime.InteropServices.ComTypes;
namespace?embeddedExcel
{
public?partial?class?ExcelWrapper?:?UserControl?{
[DllImport(“ole32.dll“)]
static?extern?int?GetRunningobjectTable(uint?reservedout?IRunningobjectTable?pprot);
[DllImport(“ole32.dll“)]
static?extern?int?CreateBindCtx(uint?reservedout?IBindCtx?pctx);
#region?Fields
private?readonly?Missing?MISS=Missing.Value;
///?Contains?a?reference?to?the?hosting?application.
private?Microsoft.Office.Interop.Excel.Application?m_XlApplication=null;
///?Contains?a?reference?to?the?active?workbook.
private?Workbook?m_Workbook=null;
private?bool?m_ToolBarVisible=false;
private?Office.CommandBar?m_StandardCommandBar=null;
///?Contains?the?path?to?the?workbook?file.
private?string?m_ExcelFileName=string.Empty;
#endregion?Fields
#region?Construction
public?ExcelWrapper()?{
InitializeComponent();
}
#endregion?Construction
#region?Properties
[Browsable(false)]
public?Workbook?Workbook?{
get?{?return?m_Workbook;?}
}
[Browsable(true)Category(“Appearance“)]
public?bool?ToolBarVisible?{
get?{?return?m_ToolBarVisible;?}
set?{
if(m_ToolBarVisible==value)?return;
m_ToolBarVisible=value;
if(m_XlApplication!=null)?OnToolBarVisibleChanged();
}
}
#endregion?Properties
#region?Events
private?void?OnToolBarVisibleChanged()?{
try?{
m_StandardCommandBar.Visible=m_ToolBarVisible;
}?catch?{?}
}
private?void?OnWebBrowserExcelNavigated(object?senderWebBrowserNavigatedEventArgs?e)?{
AttachApplication();
}
//private?void?OnOpenClick(Office.CommandBarButton?Ctrlref?bool?Cancel)?{
//????if(this.OpenExcelFileDialog.ShowDialog()==DialogResult.OK)?{
//????????OpenFile(OpenExcelFileDialog.FileName);
//????}
//}
//void?OnNewClick(Office.CommandBarButton?Ctrlref?bool?Cancel)?{
//????throw?new?Exception(“The?method?or?operation?is?not?implemented.“);
//}
#endregion?Events
#region?Methods
public?void?OpenFile(string?filename)?{
//?Check?the?file?exists
if(!System.IO.File.Exists(filename))?throw?new?Exception();
m_ExcelFileName=filename;
//?Load?the?workbook?in?the?WebBrowser?control
this.WebBrowserExcel.Navigate(filenamefalse);
}
public?Workbook?GetActiveWorkbook(string?xlfile)?{
IRunningobjectTable?prot=null;
IEnumMoniker?pmonkenum=null;
try?{
IntPtr?pfetched=IntPtr.Zero;
//?Query?the?running?object?table?(ROT)
if(GetRunningobjectTable(0out?prot)!=0||prot==null)?return?null;
prot.EnumRunning(out?pmonken
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2006-10-01?16:14??bin\
?????目錄???????????0??2006-10-01?16:14??bin\Release\
?????文件???????24576??2006-10-01?16:14??bin\Release\em
?????文件???????28160??2006-10-01?16:14??bin\Release\em
?????文件????????5632??2005-09-23?06:56??bin\Release\em
?????目錄???????????0??2006-10-01?16:09??Properties\
?????文件????????1286??2006-10-01?15:54??Properties\AssemblyInfo.cs
?????目錄???????????0??2006-10-01?16:09??Properties\DataSources\
?????文件?????????601??2006-10-01?16:09??Properties\DataSources\ExcelWrapper.datasource
?????文件????????2464??2006-10-01?15:54??Properties\Resources.Designer.cs
?????文件????????5612??2006-10-01?15:54??Properties\Resources.resx
?????文件????????1005??2006-10-01?15:54??Properties\Settings.Designer.cs
?????文件?????????249??2006-10-01?15:54??Properties\Settings.settings
?????文件????????4602??2006-10-01?16:09??em
?????文件?????????914??2006-10-01?15:54??em
?????文件????????5338??2006-10-01?16:11??ExcelWrapper.cs
?????文件????????2566??2006-10-01?16:00??ExcelWrapper.designer.cs
?????文件????????6021??2006-10-01?10:05??ExcelWrapper.resx
?????文件?????????642??2006-10-01?16:14??Form1.cs
?????文件????????4014??2006-10-01?16:14??Form1.Designer.cs
?????文件????????6017??2006-10-01?16:14??Form1.resx
?????文件?????????399??2006-10-01?15:54??Program.cs
評論
共有 條評論