可愛的琪琪,開心快樂每一天
          溫馨小屋
          可愛的琪琪,開心快樂每一天
          posts - 7,comments - 39,trackbacks - 0

          1。網(wǎng)上的一種說法,關(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;

          我試過,沒有成功


          2。關(guān)閉進(jìn)程
          C#和Asp.net下excel進(jìn)程一被打開,有時(shí)就無法關(guān)閉,?? 尤其是website.對關(guān)閉該進(jìn)程有過GC、release等方法,但這些方法并不是在所有情況下均適用。? 于是提出了kill?? process的方法,?? 目前我見過的方法多是用進(jìn)程創(chuàng)建時(shí)間篩選excel.exe進(jìn)程,?? 然后kill 。???? 這樣的方法是不精確的,?? 也是不安全的,?? 通過對網(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

          posted on 2006-11-04 11:28 陳琪 閱讀(10175) 評論(16)  編輯  收藏 所屬分類: C#

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

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

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

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 西畴县| 淮阳县| 周口市| 宝山区| 广宗县| 县级市| 洛阳市| 临泽县| 泸州市| 泾川县| 治县。| 聊城市| 乐东| 梅河口市| 航空| 依兰县| 大石桥市| 灌南县| 诸暨市| 邮箱| 德令哈市| 元阳县| 闸北区| 湟中县| 高台县| 邮箱| 沙坪坝区| 二连浩特市| 双牌县| 汝南县| 秦皇岛市| 双桥区| 湾仔区| 峡江县| 吉木萨尔县| 泾阳县| 屯留县| 宝丰县| 江门市| 江都市| 三明市|