国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 保山市| 平舆县| 电白县| 阆中市| 绥棱县| 富川| 庐江县| 任丘市| 麻城市| 当雄县| 沈阳市| 台南县| 邢台市| 东阳市| 丹棱县| 鸡西市| 新泰市| 辉县市| 乌拉特前旗| 荥经县| 会昌县| 肇源县| 呼玛县| 顺昌县| 科技| 清远市| 紫云| 达孜县| 迁安市| 调兵山市| 黔江区| 淮阳县| 苍梧县| 江永县| 武夷山市| 新和县| 清徐县| 婺源县| 精河县| 萨嘎县| 博野县|