可愛的琪琪,開心快樂每一天
          溫馨小屋
          可愛的琪琪,開心快樂每一天
          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 陳琪 閱讀(10175) 評論(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
          試試哈,謝謝了啊  回復  更多評論
            

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


          網站導航:
           
          主站蜘蛛池模板: 石门县| 蓝山县| 东乡| 赫章县| 九江县| 花莲市| 青浦区| 攀枝花市| 大庆市| 凤山县| 泸西县| 阿瓦提县| 邵武市| 大关县| 巴塘县| 泾川县| 芷江| 东乌珠穆沁旗| 徐汇区| 上高县| 内丘县| 志丹县| 十堰市| 南江县| 上栗县| 沙河市| 江油市| 虎林市| 洪洞县| 清水县| 韶关市| 玉树县| 望城县| 霍城县| 乐陵市| 旺苍县| 保亭| 南和县| 屏南县| 崇义县| 古丈县|