Excel进程继续运行(.Net Framework 3.5,VS.Net 2008,VB.Net 2008). [英] Excel process continues running (.Net Framework 3.5, VS.Net 2008, VB.Net 2008).
问题描述
我正在从按钮单击事件中调用以下方法,以将数据表导出到excel.导出完成后,将退出excel应用程序对象,将其释放,然后将其分配为空.但实际上,除非关闭整个应用程序,否则它不会被释放并保持活动状态.因此,每次单击按钮进行导出时,新的excel应用程序对象都会继续运行.我该如何解决?请帮忙.问候.
如果未使用下面方法中的两行,则不会发生此问题.但是我不能忽略它们,因为它们确实是必需的.检查*标记的行.
Hi, I''m calling the following method from a button click event to export a datatable to excel. After the export is completed, the excel application object is quit, released and assigned to nothing. But in reality it''s not getting released and stays active unless the entire application is closed. So every time the button is clicked for export, a new excel application object keeps on running. How can I solve this? Please help. Regards.
The problem doesn''t occur if two of the lines from the method below are not used. But I can''t omit them as they are really needed. Check the * marked lines.
'''''' <summary>
'''''' Exports data from a datatable to excel.
'''''' </summary>
Friend Shared Sub ExportToExcel(ByVal dtbl As DataTable)
Dim exa As Excel.Application = Nothing
Dim wkb As Excel.Workbook = Nothing
Dim wks As Excel.Worksheet = Nothing
Dim intColIndex, intRowIndex As Integer
intColIndex = 0 : intRowIndex = 2
Try
exa = New Excel.Application
exa.SheetsInNewWorkbook = 1
wkb = exa.Workbooks.Add
wks = wkb.ActiveSheet
For intColIndex = 1 To dtbl.Columns.Count
wks.Cells(1, intColIndex) = dtbl.Columns(intColIndex - 1).ColumnName
Next
For Each row As DataRow In dtbl.Rows
For intColIndex = 1 To dtbl.Columns.Count
wks.Cells(intRowIndex, intColIndex) = row(intColIndex - 1)
Next
intRowIndex += 1
Next
For intColIndex = 1 To dtbl.Columns.Count
wks.Range(wks.Cells(1, intColIndex), wks.Cells(1, intColIndex)).Font.Bold = True
wks.Range(wks.Cells(1, intColIndex), wks.Cells(1, intColIndex)).Font.Underline = True
Next
''***** The problem doesn''t occur if the following two lines are not used *****
wks.Range(wks.Cells(1, 1), wks.Cells(dtbl.Rows.Count + 1, dtbl.Columns.Count)).Columns.WrapText = False
wks.Range(wks.Cells(1, 1), wks.Cells(dtbl.Rows.Count + 1, dtbl.Columns.Count)).Columns.AutoFit()
''*****************************************************************************
exa.Visible = True
exa.UserControl = True
If Not exa Is Nothing Then exa.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(wks)
wks = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject(wkb)
wkb = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject(exa)
exa = Nothing
Catch ex As Exception
wks = Nothing
wkb = Nothing
exa = Nothing
MsgBox("The following error has occurred:" & vbCrLf & ex.Message, MsgBoxStyle.Critical, "Error")
Finally
GC.Collect()
End Try
End Sub
推荐答案
首先关闭/保存所有Excel对象:
First close/save all your Excel objects:
//wks.Save() is optional
wks.Close()
//wkb.Save() is optional
wkb.Close()
Marshal.ReleaseComObject(wks)
Marshal.ReleaseComObject(wkb)
wks = Nothing
wkb = Nothing
exa.Quit()
Marshal.ReleaseComObject(exa)
exa = Nothing
GC.Collect()
GC.WaitForPendingFinalizers()
那应该可以解决的!
问候,
-MRB
That should do the trick!
Regards,
-MRB
看看 ^ ]类似问题
我包括执行流程清理的ExcelWrapper类的代码
我还解释了Excel实例为什么挂起的一个常见原因...通过按其集合访问属性来偶然增加COM引用.
我知道它在C#中,尽管您可以很容易地移植到VB
Have a look at my answer[^] to a similar issue
I included code for an ExcelWrapper class that performs process cleanup
I''ve also explained a common reason for why Excel instances hang around...accidentally incrementing COM references by accessing properties by their collections.
I know it''s in C#, you could port to VB quite easily though
这篇关于Excel进程继续运行(.Net Framework 3.5,VS.Net 2008,VB.Net 2008).的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!