C# Excel进程关闭
using System.Runtime.InteropServices; [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID); protected void Button1_Click(object sender, EventArgs e) { Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass(); excel.Workbooks.Open("d:\aaa.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); IntPtr t = new IntPtr(excel.Hwnd); int k = 0; GetWindowThreadProcessId(t, out k); System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k); p.Kill(); }
同时需要修改配置文件machine.config
<processModel enable="true" timeout="Infinite" idleTimeout="Infinite" shutdownTimeout="0:00:05" requestLimit="Infinite" requestQueueLimit="5000" restartQueueLimit="10" memoryLimit="60" webGarden="false" cpuMask="0xffffffff" userName="System" password="AutoGenerate" logLevel="Errors" clientConnectedCheck="0:00:05" comAuthenticationLevel="Connect" comImpersonationLevel="Impersonate" responseRestartDeadlockInterval="00:09:00" responseDeadlockInterval="00:03:00" maxWorkerThreads="25" maxIoThreads="25"/>
userName="machine" 改为 userName="System"
以上部分在Win2008中找不到 没有设置成功
通过设置用户权限,Asp.net模拟用户等方式任然无法杀掉Excel进程,最后的解决办法是写一个自动杀Excel进程的服务。
using System; using System.Diagnostics; using System.ServiceProcess; namespace KillExcel { public partial class KillExcel : ServiceBase { public KillExcel() { InitializeComponent(); } protected override void OnStart(string[] args) { // TODO: 在此处添加代码以启动服务。 double sleeptime = 60 * 1000; //1分钟 System.Timers.Timer t = new System.Timers.Timer(sleeptime);//实例化Timer类,设置间隔时间为10000毫秒; t.Elapsed += new System.Timers.ElapsedEventHandler(killExcel);//到达时间的时候执行事件; t.AutoReset = true;//设置是执行一次(false)还是一直执行(true); t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件; } public void killExcel(object source, System.Timers.ElapsedEventArgs e) { Process[] myProcesses; DateTime startTime; myProcesses = Process.GetProcessesByName("Excel"); //得不到Excel进程ID,暂时只能判断进程启动时间 foreach (Process myProcess in myProcesses) { startTime = myProcess.StartTime; if ((DateTime.Now-startTime).Minutes>1) { myProcess.Kill(); } } } protected override void OnStop() { } } }
上边的服务出现不稳定问题,运行一段时间以后就会,自动杀死Excel进程,不再计算Excel启动时间与当前时间是否符合设定的时间差,还没找到原因。
计时器不能稳定运行,最后使用了线程方式来定时,这次可以稳定的运行了
public partial class KillExcel : ServiceBase { private int _timeOut; private Thread _t; public KillExcel() { InitializeComponent(); int to; int.TryParse(ConfigurationManager.AppSettings["TimeOut"], out to); _timeOut = to > 0 ? to : 3; } protected override void OnStart(string[] args) { // TODO: 在此处添加代码以启动服务。 _t = new Thread(new ThreadStart(Monitor)); _t.IsBackground = true; _t.Start(); } private void Monitor() { int sleeptime = 60 * 1000 * _timeOut; while (true) { Thread.Sleep(sleeptime); killExcel(); } } public void killExcel() { Process[] myProcesses = Process.GetProcessesByName("Excel"); //得不到Excel进程ID,暂时只能判断进程启动时间 foreach (Process myProcess in myProcesses) { if ((DateTime.Now - myProcess.StartTime).Minutes > _timeOut) { myProcess.Kill(); } } GC.Collect(); } protected override void OnStop() { } }