客户端设计器:采用C#将Excel组件嵌入窗体中,确切的是将Excel单元格区域窗体进行“绑架”,采用的的事件驱动的设计模式,采用TabControl控件,当控件初始化后,委托调用application的workbook_open事件,此事件里处理“绑架”,出现的问题为 如果此Tab正常打开,然后用IE浏览器在打开一个Excel文件,然后关闭浏览器,Workbook集合里会有多出一个Ojbect,断点进去查看,此Objcet无正常路径,FullName属性也是Object。
部分代码 ,重写TabControl控件
TabControl部分代码
#region Win32 API [DllImport("user32.dll")]
public static extern int FindWindow(string strclassName, string strWindowName); [DllImport("user32.dll")]
static extern int SetParent(int hWndChild, int hWndNewParent); [DllImport("user32.dll")]
static extern bool ShowWindow(int hWnd, uint nCmdShow); [DllImport("user32.dll", EntryPoint = "MoveWindow")]
static extern bool MoveWindow(
int Wnd,
int X,
int Y,
int Width,
int Height,
bool Repaint
); [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
static extern bool SetWindowPos(
int hWnd, // handle to window
int hWndInsertAfter, // placement-order handle
int X, // horizontal position
int Y, // vertical position
int cx, // width
int cy, // height
uint uFlags // window-positioning options
); const int SWP_DRAWFRAME = 0x20;
const int SWP_NOMOVE = 0x2;
const int SWP_NOSIZE = 0x1;
const int SWP_NOZORDER = 0x4;
const int SWP_HIDEWINDOW = 0x80;
const int SWP_SHOWWINDOW = 0x40; const int SW_HIDE = 0;
const int SW_SHOWNORMAL = 1;
const int SW_SHOWMINIMIZED = 2;
const int SW_SHOWMAXIMIZED = 3;
const int SW_MAXIMIZE = 3;
const int SW_SHOWNOACTIVATE = 4;
const int SW_SHOW = 5;
const int SW_MINIMIZE = 6;
const int SW_SHOWMINNOACTIVE = 7;
const int SW_SHOWNA = 8;
const int SW_RESTORE = 9; #endregion #region 自定义事件 public event ApplicationEventHandler ExcelTabControlClosed; protected virtual void OnExcelTabControlClosed(object sender, ApplicationEventArgs e)
{
if (ExcelTabControlClosed != null)
{
//ApplicationEventHandler applicationEventHandler = ExcelTabControlClosed as ApplicationEventHandler;
ExcelTabControlClosed(sender, e);
}
} #endregion #region 自定义属性
private int excelApplicationWnd = 0; public int ExcelApplicationWnd
{
get { return excelApplicationWnd; }
set { excelApplicationWnd = value; }
}
public ExcelControl()
{
InitializeComponent();
} public void FocusExcelApplication()
{
ShowWindow(excelApplicationWnd, 5);
}
#endregion
#region Excel和页面事件及方法
//打开Workbook事件
public void application_WorkbookOpen(object sender, EventArgs e)
{
try
{
ApplicationEventArgs applicationEventArgs = e as ApplicationEventArgs; string fileName = applicationEventArgs.Content as string;
if (fileName.StartsWith("http://")||fileName.Contains(@"Temporary Internet Files"))
return;
FindExcelApplicationWnd(); InitializeTabPage(); ControlExcelApplicationWnd(sender, e); }
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
} ExcelAppliation部分代码
private Excel.Application application; public event ApplicationEventHandler WorkbookOpened; public virtual void OnWorkbookOpened(object sender, ApplicationEventArgs e)
{
if (WorkbookOpened != null)
WorkbookOpened(this, e);
} public event ApplicationEventHandler Clear; public virtual void OnClear(object sender, ApplicationEventArgs e)
{
if (Clear != null)
Clear(this, e);
} public Excel.Application Application
{
get { return application; }
set { application = value; }
} public ExcelApplication(Excel.Application application)
{
this.application = application;
this.application.WorkbookOpen += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookOpenEventHandler(application_WorkbookOpen);
} void application_WorkbookOpen(Microsoft.Office.Interop.Excel.Workbook Wb)
{
string name = Wb.FullName;
if (name.StartsWith("http://")||name.Contains(@"Temporary Internet Files"))
return;
ApplicationEventArgs e = new ApplicationEventArgs(name, null);
OnWorkbookOpened(this, e);
} /// <summary>
/// 恢复Excel工具栏
/// </summary>
private void RestoreCommandBar()
{
try
{
application.DisplayFormulaBar = true; if (application != null && application.CommandBars != null && application.CommandBars.Count != 0)
{
IEnumerator ie = application.CommandBars.GetEnumerator();
while (ie.MoveNext() != false)
{
Office.CommandBar commandBar = (Office.CommandBar)ie.Current;
string name = commandBar.Name;
try
{
commandBar.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
catch
{
}
}
有了解的朋友帮忙解答下,代码比较繁琐,还请见谅
部分代码 ,重写TabControl控件
TabControl部分代码
#region Win32 API [DllImport("user32.dll")]
public static extern int FindWindow(string strclassName, string strWindowName); [DllImport("user32.dll")]
static extern int SetParent(int hWndChild, int hWndNewParent); [DllImport("user32.dll")]
static extern bool ShowWindow(int hWnd, uint nCmdShow); [DllImport("user32.dll", EntryPoint = "MoveWindow")]
static extern bool MoveWindow(
int Wnd,
int X,
int Y,
int Width,
int Height,
bool Repaint
); [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
static extern bool SetWindowPos(
int hWnd, // handle to window
int hWndInsertAfter, // placement-order handle
int X, // horizontal position
int Y, // vertical position
int cx, // width
int cy, // height
uint uFlags // window-positioning options
); const int SWP_DRAWFRAME = 0x20;
const int SWP_NOMOVE = 0x2;
const int SWP_NOSIZE = 0x1;
const int SWP_NOZORDER = 0x4;
const int SWP_HIDEWINDOW = 0x80;
const int SWP_SHOWWINDOW = 0x40; const int SW_HIDE = 0;
const int SW_SHOWNORMAL = 1;
const int SW_SHOWMINIMIZED = 2;
const int SW_SHOWMAXIMIZED = 3;
const int SW_MAXIMIZE = 3;
const int SW_SHOWNOACTIVATE = 4;
const int SW_SHOW = 5;
const int SW_MINIMIZE = 6;
const int SW_SHOWMINNOACTIVE = 7;
const int SW_SHOWNA = 8;
const int SW_RESTORE = 9; #endregion #region 自定义事件 public event ApplicationEventHandler ExcelTabControlClosed; protected virtual void OnExcelTabControlClosed(object sender, ApplicationEventArgs e)
{
if (ExcelTabControlClosed != null)
{
//ApplicationEventHandler applicationEventHandler = ExcelTabControlClosed as ApplicationEventHandler;
ExcelTabControlClosed(sender, e);
}
} #endregion #region 自定义属性
private int excelApplicationWnd = 0; public int ExcelApplicationWnd
{
get { return excelApplicationWnd; }
set { excelApplicationWnd = value; }
}
public ExcelControl()
{
InitializeComponent();
} public void FocusExcelApplication()
{
ShowWindow(excelApplicationWnd, 5);
}
#endregion
#region Excel和页面事件及方法
//打开Workbook事件
public void application_WorkbookOpen(object sender, EventArgs e)
{
try
{
ApplicationEventArgs applicationEventArgs = e as ApplicationEventArgs; string fileName = applicationEventArgs.Content as string;
if (fileName.StartsWith("http://")||fileName.Contains(@"Temporary Internet Files"))
return;
FindExcelApplicationWnd(); InitializeTabPage(); ControlExcelApplicationWnd(sender, e); }
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
} ExcelAppliation部分代码
private Excel.Application application; public event ApplicationEventHandler WorkbookOpened; public virtual void OnWorkbookOpened(object sender, ApplicationEventArgs e)
{
if (WorkbookOpened != null)
WorkbookOpened(this, e);
} public event ApplicationEventHandler Clear; public virtual void OnClear(object sender, ApplicationEventArgs e)
{
if (Clear != null)
Clear(this, e);
} public Excel.Application Application
{
get { return application; }
set { application = value; }
} public ExcelApplication(Excel.Application application)
{
this.application = application;
this.application.WorkbookOpen += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookOpenEventHandler(application_WorkbookOpen);
} void application_WorkbookOpen(Microsoft.Office.Interop.Excel.Workbook Wb)
{
string name = Wb.FullName;
if (name.StartsWith("http://")||name.Contains(@"Temporary Internet Files"))
return;
ApplicationEventArgs e = new ApplicationEventArgs(name, null);
OnWorkbookOpened(this, e);
} /// <summary>
/// 恢复Excel工具栏
/// </summary>
private void RestoreCommandBar()
{
try
{
application.DisplayFormulaBar = true; if (application != null && application.CommandBars != null && application.CommandBars.Count != 0)
{
IEnumerator ie = application.CommandBars.GetEnumerator();
while (ie.MoveNext() != false)
{
Office.CommandBar commandBar = (Office.CommandBar)ie.Current;
string name = commandBar.Name;
try
{
commandBar.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
catch
{
}
}
有了解的朋友帮忙解答下,代码比较繁琐,还请见谅
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货