用excel.dll生成excel文件后,不能及时的结束excel进程,用下面方法结束了excel进程。


1
2
using System.Runtime.InteropServices;
3
using System.Reflection;
4
5
6
API用来发送消息#region API用来发送消息
7
private object missing = Missing.Value;
8
/**//// <summary>
9
///API用来发送消息
10
/// </summary>
11
[DllImport("user32.dll", CharSet=CharSet.Auto)]
12
private static extern int SendMessage(int hWnd, int msg, string wParam, string lParam);
13
[ DllImport ( "user32" ) ]
14
private static extern uint FindWindow ( string lpClassName ,System.IntPtr WindowName ) ;
15
16
[DllImport("user32")]
17
private static extern uint GetWindowThreadProcessId(uint hwnd,ref uint lpdwProcessId);
18
19
[DllImport("kernel32")]
20
private static extern uint OpenProcess(uint dwDesiredAccess, uint bInheritHandle, uint dwProcessId);//(0xF0000 | 0x100000| 0xFFF)=ALL RIGHTS
21
[DllImport("kernel32")]
22
private static extern uint TerminateProcess(uint hProcess, uint uExitCode);
23
24
[DllImport("kernel32.dll", SetLastError=true)]
25
private static extern int WinExec ( string lpCmdLine, int nCmdShow) ;
26
27
/**//// <summary>
28
/// 打开浏览器
29
/// </summary>
30
[DllImport("shell32.dll", EntryPoint="ShellExecute", CharSet=CharSet.Auto)]
31
private static extern int ShellExecute(IntPtr hwnd,string lpOperation,string lpFile,string lpParameters,string lpDirectory,int nShowCmd);
32
#endregion
33
34
35
36
/**//// <summary>
37
/// 关闭所有EXCEL进程
38
/// </summary>
39
public void CloseAllExcel()
40
{
41
for(int i=0;i<1000;i++)
42
{
43
uint h=FindWindow("XLMAIN",System.IntPtr.Zero);
44
uint p=0;
45
if(h>0)
46
{
47
GetWindowThreadProcessId(h,ref p);
48
uint tmp=0x0001;
49
if(p!=0)
50
TerminateProcess(OpenProcess(tmp,0,p),0xFFFFFFFF);
51
}
52
else
53
break;
54
// SendMessage(handle, 0x10,"","");
55
}
56
}
57
58


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

50

51

52

53

54

55

56

57

58
