可愛(ài)的琪琪,開(kāi)心快樂(lè)每一天
          溫馨小屋
          可愛(ài)的琪琪,開(kāi)心快樂(lè)每一天
          posts - 7,comments - 39,trackbacks - 0

          1。網(wǎng)上的一種說(shuō)法,關(guān)閉range,關(guān)閉worksheet,關(guān)閉workbook,最后關(guān)閉excel
          System.Runtime.InteropServices.Marshal.ReleaseComObject(Range);
          2System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
          3System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);

          Range = null;
          myBook = null;
          myExcel = null;

          我試過(guò),沒(méi)有成功


          2。關(guān)閉進(jìn)程
          C#和Asp.net下excel進(jìn)程一被打開(kāi),有時(shí)就無(wú)法關(guān)閉,?? 尤其是website.對(duì)關(guān)閉該進(jìn)程有過(guò)GC、release等方法,但這些方法并不是在所有情況下均適用。? 于是提出了kill?? process的方法,?? 目前我見(jiàn)過(guò)的方法多是用進(jìn)程創(chuàng)建時(shí)間篩選excel.exe進(jìn)程,?? 然后kill 。???? 這樣的方法是不精確的,?? 也是不安全的,?? 通過(guò)對(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)上說(shuō)避免使用GC.Collect 方法 (),因?yàn)闀?huì)導(dǎo)致整個(gè)clr進(jìn)行g(shù)c,影響你的性能.所以我也沒(méi)有調(diào)用GC.Collect

          posted on 2006-11-04 11:28 陳琪 閱讀(10176) 評(píng)論(16)  編輯  收藏 所屬分類(lèi): C#

          FeedBack:
          # re: c#操作excel后關(guān)閉excel.exe的方法
          2007-03-19 15:55 | kennba
          以上代碼如果用在后期綁定時(shí),
          應(yīng)該怎么改呀?  回復(fù)  更多評(píng)論
            
          # re: c#操作excel后關(guān)閉excel.exe的方法
          2007-03-19 15:58 | kennba
          object objApp_Late;

          Type objClassType;
          objClassType = Type.GetTypeFromProgID("Excel.Application");
          objApp_Late = Activator.CreateInstance(objClassType);

          IntPtr t = new IntPtr(objApp_Late.Hwnd);

          這么寫(xiě)不對(duì),應(yīng)該怎么辦呢?

            回復(fù)  更多評(píng)論
            
          # re: c#操作excel后關(guān)閉excel.exe的方法
          2007-04-05 11:12 | 陳琪
          主要是要找到excel進(jìn)程對(duì)應(yīng)的進(jìn)程號(hào),然后通過(guò)
          System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
          p.Kill();
          把進(jìn)程殺死。
          你上面所寫(xiě)的代碼中并沒(méi)有打開(kāi)excel文件吧  回復(fù)  更多評(píng)論
            
          # re: c#操作excel后關(guān)閉excel.exe的方法
          2007-04-13 11:37 | eddy
          非常感激,好好用  回復(fù)  更多評(píng)論
            
          # re: c#操作excel后關(guān)閉excel.exe的方法
          2007-04-15 18:05 | tank414
          @eddy
          謝謝兄弟夸獎(jiǎng),嘿嘿  回復(fù)  更多評(píng)論
            
          # re: c#操作excel后關(guān)閉excel.exe的方法
          2007-05-23 12:32 | 養(yǎng)徐徐
          謝謝關(guān)掉了  回復(fù)  更多評(píng)論
            
          # re: c#操作excel后關(guān)閉excel.exe的方法[未登錄](méi)
          2007-09-24 18:41 | Jet
          怎么我那么做 k老是等于0啊  回復(fù)  更多評(píng)論
            
          # re: c#操作excel后關(guān)閉excel.exe的方法[未登錄](méi)
          2008-01-18 17:14 |
          我也是K一直為0....是ASP.NET....請(qǐng)求幫助啊..知道的請(qǐng)發(fā)我郵箱
          o_ogrowth@hotmail.com
          謝謝了。  回復(fù)  更多評(píng)論
            
          # re: c#操作excel后關(guān)閉excel.exe的方法
          2008-07-04 14:45 | 陳云
          # re: c#操作excel后關(guān)閉excel.exe的方法
          2008-08-02 10:37 | 一個(gè)人喝醉
          方法是很好,可發(fā)布后就不能關(guān)掉了,好像要設(shè)置什么權(quán)限吧  回復(fù)  更多評(píng)論
            
          # re: c#操作excel后關(guān)閉excel.exe的方法
          2009-02-11 15:27 | 孤城
          不錯(cuò)不錯(cuò),我收藏了~~  回復(fù)  更多評(píng)論
            
          # re: c#操作excel后關(guān)閉excel.exe的方法
          2009-08-07 15:32 | 陳卓
          用了你的方法怎么在我出現(xiàn)
          無(wú)法再DLL“user32.dll”找到名為“GetWindowThreadProcessId”的入口點(diǎn)  回復(fù)  更多評(píng)論
            
          # re: c#操作excel后關(guān)閉excel.exe的方法
          2009-08-31 14:19 | 老王
          寫(xiě)的太好了 非常感謝 呵呵  回復(fù)  更多評(píng)論
            
          # re: c#操作excel后關(guān)閉excel.exe的方法
          2009-10-30 15:44 | 關(guān)閉excel
          一發(fā)布就用不了,拒絕訪問(wèn)!!!不知道是不是權(quán)限問(wèn)題,誰(shuí)解決了?  回復(fù)  更多評(píng)論
            
          # re: c#操作excel后關(guān)閉excel.exe的方法
          2009-11-30 15:47 | nbbnmm
          謝謝,真的搞定了。  回復(fù)  更多評(píng)論
            
          # re: c#操作excel后關(guān)閉excel.exe的方法[未登錄](méi)
          2011-07-15 08:27 | liu
          試試哈,謝謝了啊  回復(fù)  更多評(píng)論
            

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 轮台县| 华阴市| 深水埗区| 大渡口区| 福鼎市| 黎城县| 大化| 应城市| 无锡市| 仁布县| 固安县| 恩施市| 嘉黎县| 沧源| 遂昌县| 宣化县| 九江县| 潢川县| 牟定县| 北京市| 巴彦淖尔市| 准格尔旗| 庐江县| 松溪县| 天台县| 邢台县| 安福县| 宁夏| 固阳县| 江城| 中卫市| 武汉市| 云霄县| 洛南县| 浦城县| 大邑县| 定远县| 贵定县| 凤城市| 平顶山市| 澄迈县|