小弟现在通过程序打开一个excel进程,此时我关闭excel不关闭程序,发现进程excel.exe并没有被关闭。此时如果程序再开excel就会又添加一个excel。
小弟现在想在程序中控制excel进程。
如果关闭excel的话,就把excel.exe进程释放,否则不用考虑。
现小弟的处理的方法:
1,想在开excel的时候,就检测是否有excel的进程,而该进程对应的excel文件又没打开的。就关闭,否则新开一个excel。
2,想让e xcel关闭的时候就自动把进程释放。
如何实现??
另如何添加这个excel进程到一个arraylist中??
小弟现在想在程序中控制excel进程。
如果关闭excel的话,就把excel.exe进程释放,否则不用考虑。
现小弟的处理的方法:
1,想在开excel的时候,就检测是否有excel的进程,而该进程对应的excel文件又没打开的。就关闭,否则新开一个excel。
2,想让e xcel关闭的时候就自动把进程释放。
如何实现??
另如何添加这个excel进程到一个arraylist中??
Process[] procs = Process.GetProcessesByName("Excel");
arr.AddRange(procs);
try
{
Application.Run(new MainForm());
}
catch (Exception e)
{
ExceptionForm.Show(e);
}
// 杀掉死掉的 Excel 进程
procs = Process.GetProcessesByName("Excel");
foreach (Process proc in procs)
{
if (arr.IndexOf(proc) < 0)
{
proc.Kill();
}
} Process.GetCurrentProcess().Kill();
{
System.Diagnostics.Process myproc= new System.Diagnostics.Process();
//得到所有打开的进程
try{
foreach (Process thisproc in Process.GetProcessesByName(processName)) {
if(!thisproc.CloseMainWindow()){
thisproc.Kill();
}
}
}
catch(Exception Exc)
{
msg.Text+= "杀死" + processName + "失败!";
}
}
http://dotnet.aspx.cc/ShowDetail.aspx?id=299D1529-59A3-42F9-77A7-7BF353754FEA
老孟高手的做法
{
System.Diagnostics.Process myproc= new System.Diagnostics.Process();
//得到所有打开的进程
try
{
foreach (Process thisproc in Process.GetProcessesByName(processName))
{
if(!thisproc.CloseMainWindow())
{
thisproc.Kill();
}
MessageBox.Show("杀死" + processName + "成功!");
}
}
catch(Exception Ex)
{
MessageBox.Show("杀死" + processName + "失败!");
}
}
void DoSomething(){ ... HandleExcel(); GC.Collect() ;}void HandleExcel(){Excel.Application myExcel = new Excel.Application() ;...myExcel.Quit();}
保证GC.Collect() 收集垃圾前释放EXCEL对象极其引用,确保垃圾回收器可以回收这些资源。直接杀Excel进程,但是这样有很大的问题(有可能杀掉用户自己打开的Excel)。后来用上面的方式后就好了。本方法在WinForm下的正常,asp.net中在服务器端执行Excel好像不是很好。
using System.Runtime.InteropServices;
namespace ProduceDayReport
{
public class ExceptionReport : System.Windows.Forms.Form
{
..................
private const int WM_CLOSE=0x0010;
private const int WM_QUIT=0x0012;
private const int WM_DESTROY=0x0002;
.....................................
..............
delegate DataTable AsynchronousManipulateDelegage();
private AsynchronousManipulateDelegage amd;[DllImport("user32.dll",CharSet=CharSet.Unicode)]
public static extern IntPtr PostMessage(IntPtr hwnd,int wMsg,IntPtr wParam,IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool MessageBeep(BeepType beepType);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);.....................//关键是下面的话excel.DisplayAlerts =false;
PostMessage(excel.HWND,WM_CLOSE,new IntPtr(0),new IntPtr(0));
PostMessage(excel.HWND,WM_DESTROY,new IntPtr(0),new IntPtr(0));///////////
以上是我从程序内拷贝的一部分,希望大家知道该么做!
如果你用的不是officeXP,则在打开EXCEL后,做这样的工作:
excel.Workbooks.Open (.......);
String WndClassCaption="Microsoft Excel - "+xlsName.Substring(xlsName.LastIndexOf (@"\")+1);
IntPtr hwnd_win = FindWindow("XLMAIN", WndClassCaption);
if(hwnd_win==IntPtr.Zero)
{
hwnd_win = FindWindow("MS-SDIa",xlsName.Substring(xlsName.LastIndexOf (@"\")+1));
if(hwnd_win==IntPtr.Zero)
{MessageBox.Show ("没找到窗口类 "+xlsName.Substring(xlsName.LastIndexOf (@"\")+1));
return null;
}
}
...................
并作如下更改:
excel.DisplayAlerts =false;
PostMessage((IntPtr)hwnd_win,WM_CLOSE,new IntPtr(0),new IntPtr(0));
PostMessage((IntPtr)hwnd_win,WM_DESTROY,new IntPtr(0),new IntPtr(0));
JasonHeung(拥有一切也只不过这样笑着哭)
smx717616(C#初学者…)
这样把其他不是程序打开的excel关闭了。。不好。 LoveCherry(最讨厌你们这些要代码的了,一点技术含量都灭有)
这样只能在让程序不同监听程序是否关闭才能释放excel进程。而我想是不是可以让excel一关闭的时候就释放,就和事件响应一样?redbb(....Dotneter....欢迎猎头与我联系...)
Excel是一个COM 对象,COM对象有一个引用计数的概念,只要计数不为0,Excel这个进程外COM对象就不会释放。
能不能举个例子来抓这个引数,和修改这个引数?不过这方法也和LoveCherry(最讨厌你们这些要代码的了,一点技术含量都灭有) 一样。需要监视进程窗口关闭了没有?
heiding(黑麒麟) 提出的问题,强顶,期待高手帮忙解决哦。。