简体   繁体   English

Excel无法关闭

[英]Excel doesn't close

I do everything. 我什么都做 But "excel.exe" process doesn't close. 但是“ excel.exe”进程无法关闭。 If do Excel.Visible = true , window's excel close. 如果执行Excel.Visible = true ,则窗口的excel关闭。 But not process. 但是没有过程。 Why? 为什么?

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();

This work for me: 这项工作对我来说:

Release (no Debug) 发布(无调试)

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();

if Debug - doesn't work. 如果调试-不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM