读书人

winForm中读、写Excel时采用后期绑定

发布时间: 2012-01-30 21:15:58 作者: rapoo

winForm中读、写Excel时,采用后期绑定时,如何杀掉Excel进程呀?
如题,关于杀死Excel进程的文章很多,
可是都不是采用的后期绑定方式。
有没有哪位达人做过呀,我的代码如下:
object objApp_Late;
object objBook_Late;
object objBooks_Late;
object objSheets_Late;
object objSheet_Late;
object objRange_Late;
int t=0;
int ki=0;
System.Diagnostics.Process exp2 = null;

// Get the class type and instantiate Excel.
Type objClassType;
objClassType = Type.GetTypeFromProgID( "Excel.Application ");
objApp_Late = Activator.CreateInstance(objClassType);

//程号取得
Object t2 = objApp_Late.GetType().InvokeMember( "Hwnd ", BindingFlags.GetProperty,null, objApp_Late, null );
t = (int)t2;
ki=0;
.
.
.
.
GetWindowThreadProcessId(t,out ki);
exp2 =System.Diagnostics.Process.GetProcessById(ki);
exp2.Kill();

以上代码在XP系统里没有问题,
但是win2000中运行通不过。
Object t2 = objApp_Late.GetType().InvokeMember( "Hwnd ", BindingFlags.GetProperty,null, objApp_Late, null );
这一句出错。
估计是第一个参数“Hwnd”的问题。
可能是Xp和win2000的调用方式不一样。
但不知道应该怎么写。
有没有做过的呀?

[解决办法]
参考
Office with .Net(二)之外传———“彻底干净的”关闭Office程序

http://blog.joycode.com/kaneboy/archive/2005/11/04/66364.aspx

读书人网 >C#

热点推荐