问题关闭Excel的互操作的应用程序正确地在.net中 [英] Problem closing excel interop app properly in .net

查看:116
本文介绍了问题关闭Excel的互操作的应用程序正确地在.net中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用互操作办公问题擅长的.NET。我已经尝试了很多东西,关闭Excel应用程序,工作簿和工作表我创造我的程序使用,但我始终还是注意到EXCEL.EXE在内存中。我甚至试图迫使垃圾收集器,请大家帮忙。

下面是$ C $词的使用实例的一切:

 私人mExcelApp作为Microsoft.Office.Interop.Excel.ApplicationClass
私人mWorkBook作为Microsoft.Office.Interop.Excel.Workbook
私人mWorkSheet作为Microsoft.Office.Interop.Excel.Worksheet


   公用Sub执行()

    mExcelApp =新Microsoft.Office.Interop.Excel.ApplicationClass
     如果mFirstPass然后
         mWorkBook = mExcelApp.Workbooks.Add()
         mWorkSheet = CTYPE(mWorkBook.ActiveSheet(),Microsoft.Office.Interop.Excel.Worksheet)
    其他
        mWorkBook = mExcelApp.Workbooks.Open(System.IO.Path.Combine(mFileLocation,mMTDefinition.Description&安培;_&安培; mMTDefinition.Version&安培;的.xl​​s))
         mWorkSheet = CTYPE(mWorkBook.Sheets(1),Microsoft.Office.Interop.Excel.Worksheet)
         昏暗excelRange作为Microsoft.Office.Interop.Excel.Range = mWorkSheet.UsedRange
         excelRange.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell).Activate()
         mCurrentRow = mExcelApp.ActiveCell.Row + 1

    结束如果

    这里是我尝试关闭一切

    如果mFirstPass然后
         mWorkBook.SaveAs(System.IO.Path.Combine(mFileLocation,mMTDefinition.Description&安培;_&安培; mMTDefinition.Version&安培;的.xl​​s))
         mFirstPass = FALSE
      其他
         mWorkBook.Save()
    结束如果
      一个
    mWorkBook.Close()
    mExcelApp.Quit()

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

解决方案

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

 昏暗的应用作为新Excel.Application()
    昏暗工作簿Excel.Workbook = app.Workbooks.Add()
    尝试

        昏暗T作为的Int32 = 0
        这个例子是填充使用存储在词典数据的Excel US preadsheet
        每个键在sampleData.Keys
            T + = 1
            添加工作表和转储数据。
            昏暗的板作为Excel.Worksheet = workBook.Worksheets.Add()
            sheet.Name =关键

            设置列
            对于i = 1到的sampleData(键).Columns.Count
                sheet.Cells(1,I)=的sampleData(键).Columns(I  -  1).ColumnName
            下一个

            设定数据。
            当r = 1到的sampleData(键).Rows.Count
                对于C = 1到的sampleData(键).Columns.Count
                    sheet.Cells(R + 1,C)=的sampleData(键).Rows(R  -  1).Item(C  -  1)的ToString()
                下一个C
            接下来ř
            对Marshal.ReleaseComObject(片)
        下一个

        workBook.SaveAs(fileName.xls,Excel.XlFileFormat.xlExcel8)
        workBook.Close(调用SaveChanges:= FALSE)

    最后
        app.Quit()
        对Marshal.ReleaseComObject(练习册)
        对Marshal.ReleaseComObject(表观)
        应用程序=无
    结束尝试
 

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

解决方案

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

这篇关于问题关闭Excel的互操作的应用程序正确地在.net中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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