1、winform中嵌入excel后,打开form界面,里面的excle用鼠标点不到单元格里,找不到焦点,只有按键盘,才能点到单元格上 2、在退出winform时,excel后弹出一个错误窗口,提示“遇到问题需要关闭,我们对此引起的不便表示抱歉”
已做写了退出的代码了
EA.Workbooks.get_Item(1).Close(false, missing,missing);
EA.Workbooks.Close(); wordWnd = 0;
EA.Quit(); WS = null;
EA = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(WS);
System.Runtime.InteropServices.Marshal.ReleaseComObject(WK);
System.Runtime.InteropServices.Marshal.ReleaseComObject(EA.Workbooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(EA); GC.Collect();
不知道大家有没有遇到过这样的情况
能不能跟我说下是什么原因
谢谢了
已做写了退出的代码了
EA.Workbooks.get_Item(1).Close(false, missing,missing);
EA.Workbooks.Close(); wordWnd = 0;
EA.Quit(); WS = null;
EA = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(WS);
System.Runtime.InteropServices.Marshal.ReleaseComObject(WK);
System.Runtime.InteropServices.Marshal.ReleaseComObject(EA.Workbooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(EA); GC.Collect();
不知道大家有没有遇到过这样的情况
能不能跟我说下是什么原因
谢谢了
可是不能解决我的问题
{
filename = t_filename; if (EA == null) EA = new Microsoft.Office.Interop.Excel.ApplicationClass();
try
{ if (EA != null && EA.Workbooks != null)
{
WK = EA.Workbooks.Open(filename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
}
}
catch(Exception exx)
{
MessageBox.Show("加载xls出错"+exx.Message);
} if (wordWnd == 0) wordWnd = FindWindow("XLMAIN", null);
if (wordWnd != 0)
{
SetParent(wordWnd, this.Handle.ToInt32()); object fileName = filename;
object newTemplate = false;
object docType = 1;
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
try
{
EA.Visible = true;
EA.UserControl = true;
WS = (Microsoft.Office.Interop.Excel.Worksheet)WK.ActiveSheet;
WS.Activate();
}
catch
{
MessageBox.Show("Error: do not load the document into the control until the parent window is shown!");
}
}
deactivateevents = false;
} /// <summary>
/// Close the current Document in the control --> you can
/// load a new one with LoadDocument
/// </summary>
public void CloseControl()
{
/*
* this code is to reopen Word.
*/ try
{
deactivateevents = true;
Object missing = Missing.Value;
EA.Workbooks.get_Item(1).Close(false, missing,missing);
EA.Workbooks.Close(); wordWnd = 0;
EA.Quit(); //WS = null;
//EA = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(WS);
System.Runtime.InteropServices.Marshal.ReleaseComObject(WK);
System.Runtime.InteropServices.Marshal.ReleaseComObject(EA.Workbooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(EA); GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
//this.KillProcess("EXCEL");
deactivateevents = false;
}
catch(Exception ex)
{
//MessageBox.Show("1卸载控件失败"+ex.Message);
}
}
1.使用user32.dll直接将窗体嵌入:
[DllImport("user32.dll")]
public static extern int FindWindow(string strclassName, string strWindowName); [DllImport("user32.dll")]
static extern int SetParent(int hWndChild, int hWndNewParent);
但是遇到了与你同样的问题:工作表几乎所有功能不能在程序之外操作(用代码操作是可以的),我分析是因为进程占用问题,主进程完全占用了Excel.exe 导致其他外部操作无效(看起来就是工作表死画面)。我没辙。
2.使用浏览器控件嵌入Excel解决:这可以实现我几乎所有需求(外面操作,隐藏和显示部分Excel菜单,保存等),但存在至今无法解决的问题:如何在这种情况下使用 ExcelApp.commandbars.add(...) 新增右键菜单?我们知道Excel VBA命令是可以新增的,而且Excel在弹出打开的时候使用C# ExcelApp.commandbars.add(...) 也可以实现新增菜单!但是嵌入后就不行了!官方有部分人回答是因为浏览器控件和DSOFramer控件属于WEB方案,因此部分VBA功能在这种情况下不可用!~~~~~晕了。
另外:这种情况下调试到ExcelApp.commandbars.add(...) 这句时会跳出。3.采用dsoframer.ocx控件作为容器(官方有下载),然后嵌入打开Excel,这种方法与方法2基本一致,似乎比2轻快了一点,但是依然存在那个问题!:不能新增Excel菜单! 这种情况下调试到ExcelApp.commandbars.add(...) 这句时正常通过,但是界面上没有效果!!!!! 再次晕厥~~~~~~------------------------------
以上供同探讨!