比Process.Kill()更好的选择

Rab*_*bin 4 c# visual-studio-2005

有没有比杀死()一个归档相同结果的过程更好的方法.当我杀死()Excel进程时,下次打开任何Excel工作表时,会打开"文档恢复"侧边栏,我不想这样做.

djd*_*d87 5

添加.Net引用Microsoft.Office.Interop.Excel.然后看看我敲了下面的代码:

Microsoft.Office.Interop.Excel.ApplicationClass _excel;
Microsoft.Office.Interop.Excel.Workbook _workBook;

private void Form1_Load(object sender, EventArgs e)
{
    _excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
    _excel.Visible = true;

    // Open the workbook
    _workBook = _excel.Workbooks.Open(@"DataSheet.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);

}

private void btn_Close_Click(object sender, EventArgs e)
{

    GC.Collect();
    GC.WaitForPendingFinalizers();

    _workBook.Close(false, Type.Missing, Type.Missing);
    _excel.Quit();

    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_workBook);
    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_excel);

}
Run Code Online (Sandbox Code Playgroud)