Vb.net 使用范围时不关闭excel的Visual Basic
在之前的一篇文章中,有一种经过实践检验的方法 我对此有一个问题,正如它所写的,它不适合我 VB 2010,Excel 2010 释放对象时,我的Excel实例未从任务管理器中清除。然而,如果我注释掉对range的所有引用,它就会工作 为什么靶场会把事情搞砸,我该如何解决Vb.net 使用范围时不关闭excel的Visual Basic,vb.net,excel,com,Vb.net,Excel,Com,在之前的一篇文章中,有一种经过实践检验的方法 我对此有一个问题,正如它所写的,它不适合我 VB 2010,Excel 2010 释放对象时,我的Excel实例未从任务管理器中清除。然而,如果我注释掉对range的所有引用,它就会工作 为什么靶场会把事情搞砸,我该如何解决 Imports Microsoft.Office.Interop Module Module1 Sub Main() Dim xlApp As New excel.Application Dim xlW
Imports Microsoft.Office.Interop
Module Module1
Sub Main()
Dim xlApp As New excel.Application
Dim xlWorkBook As excel.Workbook
Dim xlWorkSheet As excel.Worksheet
Dim xlRange As excel.Range
'Dim misValue As Object = System.Reflection.Missing.Value
xlApp.DisplayAlerts = False
xlWorkBook = xlApp.Workbooks.Add
xlWorkSheet = DirectCast(xlWorkBook.Sheets("sheet1"), excel.Worksheet)
xlApp.Visible = True
Dim alphabet() As Char = "abcdefghijklmnopqrstuvwxyz".ToUpper.ToCharArray
xlRange = xlWorkSheet.Range("B2:C23")
xlRange.Select()
xlWorkSheet.Paste()
xlRange.Borders(Excel.XlBordersIndex.xlDiagonalDown).LineStyle = Excel.XlLineStyle.xlLineStyleNone
xlRange.Borders(Excel.XlBordersIndex.xlDiagonalUp).LineStyle = Excel.XlLineStyle.xlLineStyleNone
With xlRange.Borders(Excel.XlBordersIndex.xlEdgeLeft)
.LineStyle = Excel.XlLineStyle.xlContinuous
.ColorIndex = 1 'black
.TintAndShade = 0
.Weight = Excel.XlBorderWeight.xlMedium
End With
With xlRange.Borders(Excel.XlBordersIndex.xlEdgeTop)
.LineStyle = Excel.XlLineStyle.xlContinuous
.ColorIndex = 1 'black
.TintAndShade = 0
.Weight = Excel.XlBorderWeight.xlMedium
End With
With xlRange.Borders(Excel.XlBordersIndex.xlEdgeBottom)
.LineStyle = Excel.XlLineStyle.xlContinuous
.ColorIndex = 1 'black
.TintAndShade = 0
.Weight = Excel.XlBorderWeight.xlMedium
End With
With xlRange.Borders(Excel.XlBordersIndex.xlEdgeRight)
.LineStyle = Excel.XlLineStyle.xlContinuous
.ColorIndex = 1 'black
.TintAndShade = 0
.Weight = Excel.XlBorderWeight.xlMedium
End With
With xlRange.Borders(Excel.XlBordersIndex.xlInsideVertical)
.LineStyle = Excel.XlLineStyle.xlContinuous
.ColorIndex = 1 'black
.TintAndShade = 0
.Weight = Excel.XlBorderWeight.xlThin
End With
With xlRange.Borders(Excel.XlBordersIndex.xlInsideHorizontal)
.LineStyle = Excel.XlLineStyle.xlContinuous
.ColorIndex = 1 'black
.TintAndShade = 0
.Weight = Excel.XlBorderWeight.xlThin
End With
xlWorkBook.SaveAs(Filename:="e:\Word1.xls", FileFormat:=56)
xlWorkBook.Close()
'xlRange = Nothing
xlApp.Quit()
ReleaseObject(xlRange)
ReleaseObject(xlWorkSheet)
ReleaseObject(xlWorkBook)
ReleaseObject(xlApp)
End Sub
Private Sub ReleaseObject(ByVal obj As Object)
Try
Dim intRel As Integer = 0
Do
intRel = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
Loop While intRel > 0
Debug.Print("Final Released obj # " & intRel)
Catch ex As Exception
Debug.Print("Error releasing object" & ex.ToString)
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
End Module
您是否在releaseobject上设置了断点以查看是否抛出了任何异常?同样,在releasecomobject行之后,您是否可以放置GC.Collect和GC.WaitForPendingFinalizers…是的,我已经放置了断点。没有例外。所有版本都很好,最终发布的obj 0 Excel在我退出程序之前不会消失。将WaitForPendingFinalizers放在gc.collect之后没有什么区别。当我在工作中启动时,我会看看我能做些什么来复制这个问题…尝试了一些进一步的测试。如果我注释掉所有的.Borders部分,它就可以工作了。一次将这些添加回一个中可以看出,最多3.Borders仍然可以让最终版本正常工作。但是,如果我有第四个.Borders Excel,在应用程序退出之前,它不会从任务管理器中消失。。。为什么?如何释放边界?