Excel无法关闭

时间:2019-05-30 05:31:26

标签: c# excel

我什么都做。但是“ excel.exe”进程无法关闭。 如果这样做Excel.Visible = true,则窗口的excel关闭。但是没有过程。为什么?

Excel.Application excel = new Excel.Application();
Excel.Workbooks books = excel.Workbooks;
Excel.Workbook book = books.Add();
Excel.Sheets sheets = book.Sheets;
Excel.Worksheet sheet = sheets.Item[1];
foreach (Contract c in list){
     //sheet.Cells[i, 1] = some str;}
book.Close(true, @"some path");
books.Close();
excel.Quit();
Marshal.ReleaseComObject(sheet);
Marshal.FinalReleaseComObject(sheet);
Marshal.ReleaseComObject(sheets);
Marshal.FinalReleaseComObject(sheets);
Marshal.ReleaseComObject(book);
Marshal.FinalReleaseComObject(book);
Marshal.ReleaseComObject(books);
Marshal.FinalReleaseComObject(books);
Marshal.ReleaseComObject(excel);
Marshal.FinalReleaseComObject(excel);

GC.Collect();
GC.WaitForPendingFinalizers();
Console.Read();

1 个答案:

答案 0 :(得分:0)

为我工作:

发布(不调试)

using Microsoft.Office.Interop.Excel;
//..
Excel.Application excel = new Excel.Application();
Workbook book = excel.Workbooks.Add();
Worksheet sheet = book.Sheets.Item[1];
try
{
    //int i = 2;
    //foreach ()
    //{
    //sheet.Cells[i, 1] = ...;
    //i++;
    //}
}
catch { }
finally
{
    book.Close(true, @"path");
    excel.Quit();
}
Console.Read();

如果调试-不起作用。