使用Excel Interop关闭Excel应用程序,不保存消息 [英] Closing Excel application with Excel Interop without save message
问题描述
我正在编写一个打开和关闭Excel应用程序,然后打开Excel工作簿和工作表的方法。完成工作后,我关闭了应用程序和工作簿。
我的问题是,这个方法可以重复调用多次,当工作表和应用程序关闭时,我从Excel应用程序得到一个消息,如果我想保存我所做的更改。该方法被卡住,直到我按否,但我不能告诉用户每次按它。
如何关闭应用程序而不收到此消息或至少用代码来解答?
这些是我用来关闭应用程序和工作簿的方法:
private void FreeWorkSheetResources(Excel.Worksheet sheet)
{
Marshal.ReleaseComObject(sheet);
sheet = null;
}
private void FreeWorkBookResources()
{
if(_excelSheets!= null)
{
Marshal.ReleaseComObject(_excelSheets);
_excelSheets = null;
}
_excelWorkBook.Close();
Marshal.ReleaseComObject(_excelWorkBook);
_excelWorkBook = null;
}
private void FreeApplicationResources()
{
_app.Quit();
Marshal.ReleaseComObject(_app);
_app = null;
GC.Collect();
}
这是我使用它们的方式:
if(_app == null)
{
_app = new Excel.Application();
}
if(_excelWorkBook == null)
{
_excelWorkBook = GetWorkBook(_filePath);
}
....
....
FreeWorkBookResources();
FreeApplicationResources();
关闭Excel WorkBook时,可以使用以下语句:
object misValue = System.Reflection.Missing.Value;
xlWorkBook.Close(false,misValue,misValue);
I am working with Excel Interop COM object. I am writing a method in which I am opening and closing an Excel application followed by opening an Excel workbook and sheet. After I'm done working on them I am closing both the application and workbook. My problem is that this method can be called repeatedly several times and when the sheet and app are closing I get from the Excel application a message if I want to save the changes I've made. The method get stuck until I press "No" but I can't tell the user to press it every time.
How can I close the application without receiving this message or at least answer it by code?
These are the methods I'm using to close the application and workbook:
private void FreeWorkSheetResources(Excel.Worksheet sheet)
{
Marshal.ReleaseComObject(sheet);
sheet = null;
}
private void FreeWorkBookResources()
{
if (_excelSheets != null)
{
Marshal.ReleaseComObject(_excelSheets);
_excelSheets = null;
}
_excelWorkBook.Close();
Marshal.ReleaseComObject(_excelWorkBook);
_excelWorkBook = null;
}
private void FreeApplicationResources()
{
_app.Quit();
Marshal.ReleaseComObject(_app);
_app = null;
GC.Collect();
}
And this is the way I'm using them:
if (_app == null)
{
_app = new Excel.Application();
}
if (_excelWorkBook == null)
{
_excelWorkBook = GetWorkBook(_filePath);
}
....
....
FreeWorkBookResources();
FreeApplicationResources();
While Closing Your Excel WorkBook you can use following Statements:
object misValue = System.Reflection.Missing.Value;
xlWorkBook.Close(false, misValue, misValue);
这篇关于使用Excel Interop关闭Excel应用程序,不保存消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!