在 .net 中正确关闭 Excel 互操作应用程序时出现问题

发布于 2024-10-19 07:46:08 字数 1588 浏览 2 评论 0原文

我在 .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()

I am having issues using interop with office excel in .net. I have tried a lot of things to close the excel application, workbook and worksheets i create to use in my program but i always notice the excel.exe still in memory. I have even tried forcing the garbage collector, please help.

Here is the code i use to instantiate everything:

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奶茶白久 2024-10-26 07:46:08

基本思想是为创建的每个 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

The basic idea is to call Marshal.ReleaseComObject for every COM object created.

    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
苦妄 2024-10-26 07:46:08

由于 Excel 应用程序是非托管 COM 对象,因此回收托管内存可能不起作用。我已经使用

Marshal.ReleaseComObject(mExcelApp);

并取得了一些成功。

As the excel application is a unmanaged COM object, reclaiming the managed memory might not work. I've used

Marshal.ReleaseComObject(mExcelApp);

with some success.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文