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

          1。網上的一種說法,關閉range,關閉worksheet,關閉workbook,最后關閉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。關閉進程
          C#和Asp.net下excel進程一被打開,有時就無法關閉,?? 尤其是website.對關閉該進程有過GC、release等方法,但這些方法并不是在所有情況下均適用。? 于是提出了kill?? process的方法,?? 目前我見過的方法多是用進程創建時間篩選excel.exe進程,?? 然后kill 。???? 這樣的方法是不精確的,?? 也是不安全的,?? 通過對網上一些關于Api運用文章的閱讀,?? 我找到了更為直接精確找到這個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();??????????????????
          ?? }


          以上代碼百分百成功的關閉excel.exe進程
          我的做法是結合兩者,先釋放資源,然后關閉進程。
          同時網上說避免使用GC.Collect 方法 (),因為會導致整個clr進行gc,影響你的性能.所以我也沒有調用GC.Collect

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

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

          這么寫不對,應該怎么辦呢?

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

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


          網站導航:
           
          主站蜘蛛池模板: 古浪县| 新沂市| 贺州市| 尉氏县| 阳江市| 苗栗县| 香格里拉县| 宜城市| 区。| 威宁| 惠安县| 南昌县| 江安县| 巩义市| 金乡县| 南康市| 中西区| 芦山县| 遂平县| 临猗县| 柯坪县| 南宫市| 盐边县| 浦城县| 福州市| 和硕县| 陆河县| 天镇县| 崇阳县| 丰台区| 竹山县| 施甸县| 青浦区| 双桥区| 固原市| 阿荣旗| 阜新市| 内乡县| 汉川市| 马山县| 高州市|