使用Excel Interop关闭Excel应用程序而不保存消息

时间:2020-12-31 07:37:11

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.

我正在使用Excel Interop COM对象。我正在编写一种方法,在其中打开和关闭Excel应用程序,然后打开Excel工作簿和工作表。在我完成它们之后,我正在关闭应用程序和工作簿。我的问题是这个方法可以反复调用几次,当工作表和应用程序关闭时,如果我想保存我所做的更改,我会从Excel应用程序中获得一条消息。该方法一直卡住,直到我按“否”,但我不能告诉用户每次按下它。

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

5 个解决方案

#1


7  

While Closing Your Excel WorkBook you can use following Statements:

关闭Excel工作簿时,您可以使用以下语句:

object misValue = System.Reflection.Missing.Value;
xlWorkBook.Close(false, misValue, misValue);

#2


2  

Do this when closing workbook.

关闭工作簿时执行此操作。

_excelWorkBook.Close(true);

#3


2  

Try this:

尝试这个:

excelApp.DisplayAlerts = False;
//save and close your workbook
excelApp.DisplayAlerts = True;

#4


2  

My problem was similar. I needed to generate Excel Sheet, then convert it to PDF. My problem was that the Excel App was displaying and notifying me to save before Exit. The solution was setting .Visible = flase and .DisplayAlerts = False

我的问题很相似。我需要生成Excel Sheet,然后将其转换为PDF。我的问题是Excel应用程序在退出之前显示并通知我保存。解决方案是设置.Visible = flase和.DisplayAlerts = False

Thanks to @adil

感谢@adil

#5


0  

I had this issue where the save dialog box was still displaying after opening and reading an Excel file without edit.

我有这个问题,在打开和读取Excel文件而没有编辑后仍然显示保存对话框。

Setting

设置

xlApplication.Visible = false;
xlApplication.DisplayAlerts = false;

and closing the workbook with the SaveChanges parameter as false

并使用SaveChanges参数关闭工作簿为false

xlWorkbook.Close(false, Missing.Value, Missing.Value);

didn't work either. I was working with an Excel file type of .xlsb (binary with macros), when I saved as .xlsx (without macros) the .Close method worked. Saving as .xslm (xslx with macros) gave me the same problem of the save dialog displaying.

也没用。我正在使用Excel文件类型.xlsb(带有宏的二进制文件),当我保存为.xlsx(没有宏)时.Close方法有效。保存为.xslm(带宏的xslx)给了我同样的显示保存对话框的问题。

I turned to the MS support site on VBA for Excel and found this article, https://support.microsoft.com/en-us/help/213428/how-to-suppress-save-changes-prompt-when-you-close-a-workbook-in-excel detailing how to accomplish the close without save in a VBA macro. Adapting to C# I explicitly set the Saved property to true. The below worked for me using .xlsx, .xlsm, .xlsb file types.

我转向VBA for Excel上的MS支持站点,发现了这篇文章,https://support.microsoft.com/en-us/help/213428/how-to-suppress-save-changes-prompt-when-you- close-a-workbook-in-excel详细说明如何在不保存VBA宏的情况下完成关闭。适应C#我明确地将Saved属性设置为true。以下使用.xlsx,.xlsm,.xlsb文件类型为我工作。

        Excel.Application xlApp = new Excel.Application();
        xlApp.DisplayAlerts = false;
        xlApp.Visible = false;
        var xlWorkbook = xlApp.Workbooks.Open(fileInfo.FullName);

        // do work

        xlWorkbook.Saved = true;
        xlWorkbook.Close(false, Missing.Value, Missing.Value);
        xlApp.Quit();

#1


7  

While Closing Your Excel WorkBook you can use following Statements:

关闭Excel工作簿时,您可以使用以下语句:

object misValue = System.Reflection.Missing.Value;
xlWorkBook.Close(false, misValue, misValue);

#2


2  

Do this when closing workbook.

关闭工作簿时执行此操作。

_excelWorkBook.Close(true);

#3


2  

Try this:

尝试这个:

excelApp.DisplayAlerts = False;
//save and close your workbook
excelApp.DisplayAlerts = True;

#4


2  

My problem was similar. I needed to generate Excel Sheet, then convert it to PDF. My problem was that the Excel App was displaying and notifying me to save before Exit. The solution was setting .Visible = flase and .DisplayAlerts = False

我的问题很相似。我需要生成Excel Sheet,然后将其转换为PDF。我的问题是Excel应用程序在退出之前显示并通知我保存。解决方案是设置.Visible = flase和.DisplayAlerts = False

Thanks to @adil

感谢@adil

#5


0  

I had this issue where the save dialog box was still displaying after opening and reading an Excel file without edit.

我有这个问题,在打开和读取Excel文件而没有编辑后仍然显示保存对话框。

Setting

设置

xlApplication.Visible = false;
xlApplication.DisplayAlerts = false;

and closing the workbook with the SaveChanges parameter as false

并使用SaveChanges参数关闭工作簿为false

xlWorkbook.Close(false, Missing.Value, Missing.Value);

didn't work either. I was working with an Excel file type of .xlsb (binary with macros), when I saved as .xlsx (without macros) the .Close method worked. Saving as .xslm (xslx with macros) gave me the same problem of the save dialog displaying.

也没用。我正在使用Excel文件类型.xlsb(带有宏的二进制文件),当我保存为.xlsx(没有宏)时.Close方法有效。保存为.xslm(带宏的xslx)给了我同样的显示保存对话框的问题。

I turned to the MS support site on VBA for Excel and found this article, https://support.microsoft.com/en-us/help/213428/how-to-suppress-save-changes-prompt-when-you-close-a-workbook-in-excel detailing how to accomplish the close without save in a VBA macro. Adapting to C# I explicitly set the Saved property to true. The below worked for me using .xlsx, .xlsm, .xlsb file types.

我转向VBA for Excel上的MS支持站点,发现了这篇文章,https://support.microsoft.com/en-us/help/213428/how-to-suppress-save-changes-prompt-when-you- close-a-workbook-in-excel详细说明如何在不保存VBA宏的情况下完成关闭。适应C#我明确地将Saved属性设置为true。以下使用.xlsx,.xlsm,.xlsb文件类型为我工作。

        Excel.Application xlApp = new Excel.Application();
        xlApp.DisplayAlerts = false;
        xlApp.Visible = false;
        var xlWorkbook = xlApp.Workbooks.Open(fileInfo.FullName);

        // do work

        xlWorkbook.Saved = true;
        xlWorkbook.Close(false, Missing.Value, Missing.Value);
        xlApp.Quit();