开发Excel插件时要释放COM? [英] Release COM when developing an Excel Addin?

查看:103
本文介绍了开发Excel插件时要释放COM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解使用互操作时应释放COM对象.例如,在开发和加载项时,情况是否有些不同?这是一个循环,我很好奇Marshal.ReleaseComObject是否必要?

I understand that I should release COM objects when using interop. Are things a bit different when developing and Add-In, Excel for example? Here is a loop I have and I was curious to know if the Marshal.ReleaseComObject is necessary?

 foreach (var sheet in results.Sheets)
            {
                var newSheet = workbook.AddSheet();

                newSheet.SetSheetTabColor(sheet.TabColor);
                newSheet.SetSheetName(sheet.TabName);
                newSheet.Cells.SetFont("Calibri", 8);
                newSheet.FreezeRow(1);

                var endRow = sheet.Data.GetUpperBound(0) + 1;
                var endColumn = sheet.Data.GetUpperBound(1) + 1;

                var writeRange = newSheet.SetWriteRange(1, 1, endRow, endColumn);



                writeRange.Value2 = sheet.Data;

                newSheet.AutoFitColumns();
                newSheet.RemoveColumn(1);

                Marshal.ReleaseComObject(newSheet);

            }

此外,我还创建了带有扩展方法的库.一个例子是workbook.AddSheet() AddSheet看起来像这样:

Also, I created a library with extension methods. One example is workbook.AddSheet() AddSheet looks like this:

public static Worksheet AddSheet(this Microsoft.Office.Interop.Excel.Workbook workbook)
        {
            var sheets = workbook.Sheets;

            return sheets.Add(After: sheets[sheets.Count]);
        }

由于我要从workbook.Sheets访问sheets,因此是否必须释放该对象?如果是这样,自从我返回工作表后在哪里?我回来之前无法释放?

Since I am accessing sheets from workbook.Sheets, do I have to release of this object? If so where since I am returning a Worksheet? I can't release before I return?

这可能是一个愚蠢的问题,但是如果Marshal.ReleaseComObject在foreach范围内不是必需的,即使它仍然存在,也会疼吗?

This may be a dumb question, but if the Marshal.ReleaseComObject was not necessary in the foreach scope, does it hurt even if it is still there?

推荐答案

Marshal.ReleaseComObject仅在需要及时或以特定顺序控制COM对象的生存期时使用.对于COM对象的临时使用,我不会建议您完全使用此方法.

Marshal.ReleaseComObject is used only if you need to control the lifetime of an COM object in timely manner, or in specific order. For casual usage of COM objects i would not advice you to use this method at all.

检查这里

此方法用于显式控制COM对象的生存期 用于托管代码.您应该使用此方法释放 底层COM对象,可及时保存对资源的引用 方式或必须按特定顺序释放对象的时间.

This method is used to explicitly control the lifetime of a COM object used from managed code. You should use this method to free the underlying COM object that holds references to resources in a timely manner or when objects must be freed in a specific order.

关于第二个问题

由于我正在从工作簿中访问工作表. 这个物体?如果是这样,自从我返回工作表后在哪里?我不能 在我回来之前释放?

Since I am accessing sheets from workbook.Sheets, do I have to release of this object? If so where since I am returning a Worksheet? I can't release before I return?

.NET中的COM实现使用引用计数机制来检测是否使用了对象,因此您不必显式释放任何内容.该框架可用于此目的.

The COM implementation in .NET is using reference counting mechanism to detect if object is used or not, so NO you don't have to release anything explicitly. The framework is resposible for this.

注意: 使用适当的.Close(用于工作簿),.Quit(用于应用)方法适当地释放资源.在应用程序对象上使用.Quit()时,您将在Windows中关闭excel进程(这样将释放所有资源),并在工作簿上关闭.Close()来释放特定excel上的文件锁..等文件.

NOTE: Use the approriate .Close(for Workbook), .Quit(for Application) methods for proper release of resurces. When you use .Quit() over the application object you will close the excel process in windows (so this will release all resource), and .Close() over a Workbook to release file locks .. etc over specific excel file.

这篇关于开发Excel插件时要释放COM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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