到版本的Excel互操作COM对象的最佳途径 [英] Best Way to Release Excel Interop com Object

查看:188
本文介绍了到版本的Excel互操作COM对象的最佳途径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

专家们,请让我知道我能做到这一点的最好办法...

Experts, Please let me know the best way I can do this...

我的工作,它使用的Microsoft.Office.Interop.Excel对象库的工作表中创建在一个工作簿和数据透视表的工作表一个VB.Net应用程序。

I am working on a VB.Net application that uses the Microsoft.Office.Interop.Excel Object Library to create worksheets within a workbook and Pivot Tables within those worksheets.

我的code看起来是这样的:

My code looks something like this:

Dim ExcelApp As New Microsoft.Office.Interop.Excel.Application
Dim wbk As Microsoft.Office.Interop.Excel.Workbook = Nothing
Dim wksRawData As Microsoft.Office.Interop.Excel.Worksheet = Nothing
Dim wksPvtTbl As Microsoft.Office.Interop.Excel.Worksheet = Nothing
Dim pvtCache As Microsoft.Office.Interop.Excel.PivotCache = Nothing
Dim pvtTables As Microsoft.Office.Interop.Excel.PivotTables = Nothing
Dim pvtTable As Microsoft.Office.Interop.Excel.PivotTable = Nothing
Dim r1 As Microsoft.Office.Interop.Excel.PivotField = Nothing
Dim r2 As Microsoft.Office.Interop.Excel.PivotField = Nothing
Dim df1 As Microsoft.Office.Interop.Excel.PivotField = Nothing

Try

... Create the objects, put in the information

Catch ex As Exception
   MessageBox.Show("There was an error creating the Excel file", "Error Creating File", MessageBoxButtons.OK)
Finally

   ReleaseObject(r1)
   ReleaseObject(r2)
   ReleaseObject(df1)
   ReleaseObject(pvtTable)
   ReleaseObject(pvtTables)
   ReleaseObject(pvtCache)
   ReleaseObject(wksRawData)
   ReleaseObject(wksPvtTbl)
   ReleaseObject(wbk)

   ExcelApp.DisplayAlerts = True
   ExcelApp.Quit()
   ReleaseObject(ExcelApp)

   ExcelApp = Nothing
   wbk = Nothing
   wksRawData = Nothing
   GC.Collect()
End Try

然后,我有code(如下所示)为我发行对象:

Then I have code that looks as follows for my release objects:

Public Sub ReleaseObject(ByRef Reference As Microsoft.Office.Interop.Excel.Application)
    Dim i As Integer
    If Reference IsNot Nothing Then
        i = System.Runtime.InteropServices.Marshal.ReleaseComObject(Reference)

        While i > 0
            i = System.Runtime.InteropServices.Marshal.ReleaseComObject(Reference)
        End While

        Reference = Nothing
    End If
End Sub

Public Sub ReleaseObject(ByRef Reference As Microsoft.Office.Interop.Excel.Workbook)
    Dim i As Integer
    If Reference IsNot Nothing Then
        i = System.Runtime.InteropServices.Marshal.ReleaseComObject(Reference)

        While i > 0
            i = System.Runtime.InteropServices.Marshal.ReleaseComObject(Reference)
        End While

        Reference = Nothing
    End If
End Sub

... etc

我知道有许多解决方案在那里的这样那样的问题,但我迷失在所有不同的人知道什么最适合我目前的状况? 这是一个好办法做到这一点,如果没有,什么是更有效的方式??

I know there are MANY solutions out there for this kind of problem, but I’m getting lost in all the different ones to know what best fits my current situation… Is this a good way to do this and, if not, what is a more efficient way??

谢谢!

推荐答案

不要手动搜索和呼叫释放。在互操作过程中仍然可以运行宽松,如果应用程序崩溃灾难性的。

Don't search and call release manually. The Interop process could still run loose if the application crashes catastrophically.

我会通过的工作对象

下面是一个解决方案: http://stackoverflow.com/a/1307180/160146

Here is a solution: http://stackoverflow.com/a/1307180/160146

这篇关于到版本的Excel互操作COM对象的最佳途径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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