解析c#操作excel后關(guān)閉excel.exe的方法
更新時(shí)間:2013年07月09日 09:09:34 作者:
C#和Asp.net下excel進(jìn)程一被打開,有時(shí)就無法關(guān)閉,尤其是website.對(duì)關(guān)閉該進(jìn)程有過GC、release等方法,但這些方法并不是在所有情況下均適用
于是提出了kill process的方法,目前我見過的方法多是用進(jìn)程創(chuàng)建時(shí)間篩選excel.exe進(jìn)程,然后kill 。這樣的方法是不精確的,也是不安全的,通過對(duì)網(wǎng)上一些關(guān)于Api運(yùn)用文章的閱讀,我找到了更為直接精確找到這個(gè)process并kill的方法
以下就是代碼
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();
}
以上代碼百分百成功的關(guān)閉excel.exe進(jìn)程
我的做法是結(jié)合兩者,先釋放資源,然后關(guān)閉進(jìn)程。
同時(shí)網(wǎng)上說避免使用GC.Collect 方法 (),因?yàn)闀?huì)導(dǎo)致整個(gè)clr進(jìn)行g(shù)c,影響你的性能.所以我也沒有調(diào)用GC.Collect
以下就是代碼
復(fù)制代碼 代碼如下:
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();
}
以上代碼百分百成功的關(guān)閉excel.exe進(jìn)程
我的做法是結(jié)合兩者,先釋放資源,然后關(guān)閉進(jìn)程。
同時(shí)網(wǎng)上說避免使用GC.Collect 方法 (),因?yàn)闀?huì)導(dǎo)致整個(gè)clr進(jìn)行g(shù)c,影響你的性能.所以我也沒有調(diào)用GC.Collect
相關(guān)文章
適用于WebForm Mvc的Pager分頁組件C#實(shí)現(xiàn)
這篇文章主要為大家分享了適用于WebForm Mvc的Pager分頁組件,由C#實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2016-04-04
C#實(shí)現(xiàn)排列組合算法完整實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)排列組合算法的完整實(shí)例,文中實(shí)例主要展示了排列循環(huán)方法和排列堆棧方法,需要的朋友可以參考下2014-09-09
C# LINQ查詢表達(dá)式及對(duì)應(yīng)LAMBDA表達(dá)式的用法
這篇文章主要介紹了C# LINQ查詢表達(dá)式及對(duì)應(yīng)LAMBDA表達(dá)式的用法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-04-04
使用C#如何創(chuàng)建人名或其他物體隨機(jī)分組
文章描述了一個(gè)隨機(jī)分配人員到多個(gè)團(tuán)隊(duì)的代碼示例,包括將人員列表隨機(jī)化并根據(jù)組數(shù)分配到不同組,最后按組號(hào)排序顯示結(jié)果2025-01-01
C#生成指定范圍內(nèi)的不重復(fù)隨機(jī)數(shù)
對(duì)于隨機(jī)數(shù),大家都知道,計(jì)算機(jī)不 可能產(chǎn)生完全隨機(jī)的數(shù)字,所謂的隨機(jī)數(shù)發(fā)生器都是通過一定的算法對(duì)事先選定的隨機(jī)種子做復(fù)雜的運(yùn)算,用產(chǎn)生的結(jié)果來近似的模擬完全隨機(jī)數(shù),這種隨機(jī)數(shù)被稱 作偽隨機(jī)數(shù)。偽隨機(jī)數(shù)是以相同的概率從一組有限的數(shù)字中選取的。2015-05-05

