如何在c#中动态关闭excel文件 [英] how to close an excel file dynamically in c#
本文介绍了如何在c#中动态关闭excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法关闭raj4.xls文件。它在'工作簿'中引发错误,指出'你错过了使用指令还是程序集引用?'。这个代码是否正确关闭我的excel文件?
I am unable to close my 'raj4.xls' file. It throws an error at 'workbooks' stating 'are you missing a using directive or an assembly reference?'. Is this code correct to close my excel file?
ExcelApp.ActiveWorkbook.SaveCopyAs("C:\\Users\\RAJENDRAN\\Desktop\\raj4.xls");
ExcelApp.ActiveWorkbook.Saved = true;
object fileName = "C:\\Users\\RAJENDRAN\\Desktop\\raj4.xls";
Excel.Workbook workbook = this.Application.Workbooks.get_Item(fileName);
workbook.Close(false);
ExcelApp.Quit();
推荐答案
我认为您的问题极有可能是由使用Excel的已知问题引起的通过与.NET的互操作:在.NET代码中创建Excel对象时,会为每个对象创建COM包装器,这些包装需要在退出应用程序之前进行显式处理。请参阅:[ ^ ]。
I think it's highly probable your issue is caused by a known problem with using Excel via interop with .NET: as you create Excel objects in your .NET code, COM wrappers are created for each object, and these need to be explicitly disposed before you can quit the application. See: [^].
这篇关于如何在c#中动态关闭excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文