原文地址:http://www.cnblogs.com/feishu/archive/2010/05/08/1730797.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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进程的服务。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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启动时间与当前时间是否符合设定的时间差,还没找到原因。

计时器不能稳定运行,最后使用了线程方式来定时,这次可以稳定的运行了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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()
        {
             
        }
    }

[转]Excel关闭进程的更多相关文章

  1. 根据Excel线程句柄得到ID并且关闭进程

    [System.Runtime.InteropServices.DllImport("User32.dll", CharSet = System.Runtime.InteropSe ...

  2. C#导出Excel后关闭进程EXCEL.EXE

    在C#中使用Microsoft.Office.Interop.Execl 导出excel 表格时,将以下两个属性亩后,在导完后, Excel.exe 进程无法关闭. // excel app 是否可见 ...

  3. C# 调用word进程操作文档关闭进程

    C# 调用word进程操作文档关闭进程 作者:Jesai 时间:2018-02-12 20:36:23 前言: office办公软件作为现在主流的一款办公软件,在我们的日常生活和日常工作里面几乎每天都 ...

  4. wpf 异常处理和关闭进程

    using System; using System.Collections.Generic; using System.Configuration; using System.Data; using ...

  5. Dos命令查看端口占用及关闭进程

    1. 查看端口占用 在windows命令行窗口下执行: netstat -aon|findstr "8080" TCP 127.0.0.1:80 0.0.0.0:0 LISTENI ...

  6. Centos下查看占用端口并关闭进程方法

    1.查看端口占用情况:netstat –tlnp   (加p可以看到是哪个进程占用了端口); 也可以用grep查找对应的被占用的端口,键入netstat –tlnp | grep 3306可以看到PI ...

  7. innosetup安装之前关闭进程

    InnoSetup覆盖安装的时候可能会因为源程序正在运行而安装失败,以下脚本能够关闭原运行进程. [code] // 安装前检查关闭**进程 function InitializeSetup():Bo ...

  8. hadoop 关闭进程时报错no 进程 to stop

    前两天和朋友李天王吃饭的时候,聊到了一个hadoop的运维的很简单问题,感觉很有意思,以前也没有注意过,现在加以重现和整理.   感谢李天王的分享....   翻看了yarn-deamon.sh st ...

  9. 在CMD命令行下关闭进程的命令

    转载: [重要]在CMD命令行下关闭进程的命令━━━━━━━━━━━━━━━━━━━━━━━━━━ 方法一: 在"运行"中输入:ntsd -c q -pn 程序名字(在MS-Dos ...

随机推荐

  1. Linux网络配置之虚拟网卡的配置(ubuntu 16.04)案例

    sudo vim /etc/network/interfaces 标红的名称一定要一致 sudo vim /etc/resolv.conf  配置外网的ip(默认可以不填,系统自己获取)

  2. 用C++实现文件压缩(1.5)

    今天主要做的就是,将完成huffman编码的数据以二进制的形式写入文件中.这是个挺苦逼的活. 不过好在我以前玩过一段时间的单片机,所有能够较好的实现位运算,一位一位的将数据存放到缓冲区中,然后统一写入 ...

  3. rails generate model/resource/scaffold的区别

    If you’re just learning Ruby on Rails, you may be confused as to when to generate individual models, ...

  4. S7:享元模式 Flyweight

    运用共享技术有效的支持大量细粒度的对象. 应用场景: A.减少对相同对象的重复创建 UML: 示例代码:如果在工厂中,有用户,我们就直接调用,没有用户,我们就获取.减少对同一uid的user对象的重复 ...

  5. Python——在Python中如何使用Linux的epoll

    在Python中如何使用Linux的epoll 目录 序言 阻塞socket编程示例 异步socket的好处以及Linux epoll 带epoll的异步socket编程示例 性能注意事项 源代码 序 ...

  6. HTML ui ol dl

    <!-- 超链接target的属性 _blank 在新窗体中打开被链接文档. _self 默认. 在同样的框架中打开被链接文档. _parent 在父框架集中打开被链接文档. _top 在整个窗 ...

  7. Android-LinearLayout布局技巧(二)

    先来看看图: 布局代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...

  8. 【MyBatis学习14】MyBatis和Spring整合

    前面十几篇博文总结了mybatis在开发中的相关技术,但在实际中都是和spring整合开发的,所以这里总结一下mybatis和spring的整合方法,并在整合后进行测试. 1. 整合的环境 这都是老掉 ...

  9. 文件打开的过程——调用fd=open()时操作系统所做的工作

    fd=fopen()是一个系统调用.用于依据文件名称打开一个文件.返回该文件的文件描写叙述符,文件打开后进程便能够依据文件描写叙述符fd进行其它操作,比方读,写,关闭等操作. 各个操作系统打开文件的过 ...

  10. Atitit.编程语言新特性 通过类库框架模式增强 提升草案 v3 q27

    Atitit.编程语言新特性 通过类库框架模式增强 提升草案 v3 q27 1. 修改历史2 2. 适用语言::几乎所有编程语言.语言提升的三个渠道::语法,类库,框架,ide2 2.1. 单根继承  ...