如何关闭 - 不保存Excel / xlsm工作簿,具有自定义功能,从C# [英] How to close-without-save an Excel /xlsm workbook, w/ a custom function, from C#

查看:670
本文介绍了如何关闭 - 不保存Excel / xlsm工作簿,具有自定义功能,从C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义非时间依赖单元格功能的Excel工作簿,我使用Interop.Excel从C#WindowsForms应用程序打开。我读了四个值,不执行任何明确的更改/计算,然后从C#中关闭它。



当我尝试直接关闭(不保存)时,我得到一个保存提示。我怀疑这是因为Excel在打开时解释自动重新计算,因为创建一个变化,即使没有实际的数字或公式上的变化。我还设置了 Excel.Application.Calculation = Excel.XlCalculation.xlCalculationManual 。如何防止保存提示出现,只需关闭 - 不保存?

解决方案

使用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);

....做你的东西

xlWorkBook.Close(false,misValue,misValue);

xlApp.Quit();

关闭方法中的false表示不保存更改。


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#.

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?

解决方案

When you use the Excel objects, be sure to close the workbook like this:

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.

这篇关于如何关闭 - 不保存Excel / xlsm工作簿,具有自定义功能,从C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆