excel을 종료합니다.exe 프로세스

1389 단어 Excel
using System.Diagnostics;
using System.Runtime.InteropServices;

/// <summary>
///  ID
/// </summary>
/// <param name="hwnd">   FindWindow </param>
/// <param name="ID"> ID</param>
/// <returns></returns>
[DllImport("User32.dll", CharSet = CharSet.Auto)] 
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID); 

/// <summary>
///  Excel 
/// </summary>
/// <param name="excel"></param>
public void KillExcel(Microsoft.Office.Interop.Excel._Application excel)
{
    // 
    IntPtr ptr = new IntPtr(excel.Hwnd);
    // ID
    int processID = 0;
    GetWindowThreadProcessId(ptr, out processID);
    // 
    Process process = Process.GetProcessById(processID);
    // 
    if (!process.CloseMainWindow())// 
    {
        process.Kill();// 
    }
}

public void Dispose()
{
    System.Runtime.InteropServices.Marshal.ReleaseComObject(this.worksheet);
    this.worksheet = null;
    System.Runtime.InteropServices.Marshal.ReleaseComObject(this.workbook);
    this.workbook = null;
    if (this.application != null)
    {
        this.application.Quit();

        // Excel ( )
        this.KillExcel(this.application);

        System.Runtime.InteropServices.Marshal.ReleaseComObject(this.application);
        this.application = null;
    }
}

좋은 웹페이지 즐겨찾기