【问题标题】:Problem closing excel interop app properly in .net在.net中正确关闭excel互操作应用程序的问题
【发布时间】:2012-03-27 02:24:29
【问题描述】:

我在 .net 中使用与 office excel 互操作时遇到问题。我尝试了很多方法来关闭我创建的要在我的程序中使用的 excel 应用程序、工作簿和工作表,但我总是注意到 excel.exe 仍在内存中。我什至尝试过强制垃圾收集器,请帮忙。

这是我用来实例化所有内容的代码:

Private mExcelApp As Microsoft.Office.Interop.Excel.ApplicationClass
Private mWorkBook As Microsoft.Office.Interop.Excel.Workbook
Private mWorkSheet As Microsoft.Office.Interop.Excel.Worksheet


   Public Sub Execute()

    mExcelApp = New Microsoft.Office.Interop.Excel.ApplicationClass
     If mFirstPass Then
         mWorkBook = mExcelApp.Workbooks.Add()
         mWorkSheet = CType(mWorkBook.ActiveSheet(), Microsoft.Office.Interop.Excel.Worksheet)
    Else
        mWorkBook = mExcelApp.Workbooks.Open(System.IO.Path.Combine(mFileLocation, mMTDefinition.Description & "_" & mMTDefinition.Version & ".xls"))
         mWorkSheet = CType(mWorkBook.Sheets(1), Microsoft.Office.Interop.Excel.Worksheet)
         Dim excelRange As Microsoft.Office.Interop.Excel.Range = mWorkSheet.UsedRange
         excelRange.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell).Activate()
         mCurrentRow = mExcelApp.ActiveCell.Row + 1

    End If

    Here is how i try to close everything

    If mFirstPass Then
         mWorkBook.SaveAs(System.IO.Path.Combine(mFileLocation, mMTDefinition.Description & "_" & mMTDefinition.Version & ".xls"))
         mFirstPass = False
      Else
         mWorkBook.Save()
    End If
      a
    mWorkBook.Close()
    mExcelApp.Quit()

    mWorkSheet = Nothing
    mWorkBook = Nothing
    mExcelApp = Nothing
    System.GC.Collect()

【问题讨论】:

  • 你试过for each book in mExcelApp.Workbooks: book.saved = true: next吗?

标签: .net vb.net office-interop


【解决方案1】:

基本思想是为每个创建的 COM 对象调用 Marshal.ReleaseComObject。

    Dim app As New Excel.Application()
    Dim workBook As Excel.Workbook = app.Workbooks.Add()
    Try

        Dim t As Int32 = 0
        ' This example is filling an excel spreadsheet using data stored in a Dictionary
        For Each key In sampleData.Keys
            t += 1
            ' Add a worksheet and dump data.
            Dim sheet As Excel.Worksheet = workBook.Worksheets.Add()
            sheet.Name = key

            ' Set columns
            For i = 1 To sampleData(key).Columns.Count
                sheet.Cells(1, i) = sampleData(key).Columns(i - 1).ColumnName
            Next

            ' Set data.
            For r = 1 To sampleData(key).Rows.Count
                For c = 1 To sampleData(key).Columns.Count
                    sheet.Cells(r + 1, c) = sampleData(key).Rows(r - 1).Item(c - 1).ToString()
                Next c
            Next r
            Marshal.ReleaseComObject(sheet)
        Next

        workBook.SaveAs("fileName.xls", Excel.XlFileFormat.xlExcel8)
        workBook.Close(SaveChanges:=False)

    Finally
        app.Quit()
        Marshal.ReleaseComObject(workbook)
        Marshal.ReleaseComObject(app)
        app = Nothing
    End Try

【讨论】:

  • 非常感谢贾斯汀,这完全适合我。现在我知道如何处理非托管 com 对象了。
【解决方案2】:

由于 excel 应用程序是非托管 COM 对象,回收托管内存可能不起作用。我用过

Marshal.ReleaseComObject(mExcelApp);

取得了一些成功。

【讨论】:

  • 我会试试那个。非常感谢
猜你喜欢
  • 2016-12-28
  • 2012-10-16
  • 2019-12-24
  • 2013-02-21
  • 1970-01-01
  • 2012-03-23
  • 1970-01-01
  • 2013-07-18
  • 1970-01-01
相关资源
最近更新 更多