如何从 C# 关闭而不保存 Excel /xlsm 工作簿,带有自定义函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8813887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to close-without-save an Excel /xlsm workbook, w/ a custom function, from C#
提问by Hyman Huang
I have an Excel workbook with a custom non-time-dependent cell function that I am opening from a C# WindowsForms application using Interop.Excel. I read four values from it, perform no explicit changes/calculations, then close it from C#.
我有一个 Excel 工作簿,其中包含一个自定义的非时间相关单元格函数,我正在使用 Interop.Excel 从 C# WindowsForms 应用程序打开它。我从中读取了四个值,不执行显式更改/计算,然后从 C# 中关闭它。
When I try to directly close (without saving), I get a save prompt. I suspect this is because Excel is interpreting auto-recalculation on open as creating a change, even though nothing actually numerically or formulaically changes. I also set Excel.Application.Calculation = Excel.XlCalculation.xlCalculationManual. How do I prevent the save prompt from appearing and just close-without-save?
当我尝试直接关闭(不保存)时,出现保存提示。我怀疑这是因为 Excel 将打开时的自动重新计算解释为创建更改,即使实际上没有任何数字或公式更改。我也设置了Excel.Application.Calculation = Excel.XlCalculation.xlCalculationManual。如何防止出现保存提示并关闭而不保存?
采纳答案by Motes
When you use the Excel objects, be sure to close the workbook like this:
当您使用 Excel 对象时,请务必像这样关闭工作簿:
Excel.Application xlApp ;
Excel.Workbook xlWorkBook ;
Excel.Worksheet xlWorkSheet ;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add("Yourworkbook");
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
....do your stuff
xlWorkBook.Close(false, misValue, misValue);
xlApp.Quit();
The false in the close method indicates don't save changes.
close 方法中的 false 表示不保存更改。

