国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#利用com操作excel釋放進程的解決方法

2020-01-24 03:31:20
字體:
來源:轉載
供稿:網友

第一個

復制代碼 代碼如下:

System.Runtime.InteropServices.Marshal.ReleaseComObject(sheets);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(range);

        excelApp = null;
        wbclass = null;
        sheets = null;
        worksheet = null;
        range = null;
        GC.Collect();
        GC.WaitForPendingFinalizers();


釋放不徹底,還是有進程存在。

第二種

復制代碼 代碼如下:

//調用底層函數(shù)獲取進程標示
    [DllImport("User32.dll")]
    public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int ProcessId);
    private static void KillExcel(Microsoft.Office.Interop.Excel.Application theApp)
    {
        int id = 0;
        IntPtr intptr = new IntPtr(theApp.Hwnd);
        System.Diagnostics.Process p = null;
        try
        {
            GetWindowThreadProcessId(intptr, out id);
            p = System.Diagnostics.Process.GetProcessById(id);
            if (p != null)
            {
                p.Kill();
                p.Dispose();
            }
        }
        catch (Exception ex)
        {

        }
    }

這個方法比較好,我試過了可以關閉掉進程。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 望都县| 普陀区| 崇义县| 古田县| 曲阜市| 雷波县| 濮阳市| 烟台市| 岳阳县| 吴江市| 湘乡市| 富民县| 马边| 延庆县| 泽普县| 九龙城区| 樟树市| 汽车| 阿拉善盟| 闻喜县| 芦山县| 平度市| 博客| 平阴县| 海南省| 庆云县| 抚松县| 绵阳市| 武山县| 定南县| 抚顺县| 汶上县| 乌兰察布市| 济南市| 石台县| 邢台县| 嫩江县| 息烽县| 太原市| 盈江县| 同仁县|