gpt4 book ai didi

vb.net - 在 vb.net 中读取 Excel 文件会使 Excel 进程挂起

转载 作者:行者123 更新时间:2023-12-02 17:17:49 30 4
gpt4 key购买 nike

以下代码工作正常,但似乎让 excel.exe 实例在后台运行。我该如何正确地关闭这个子程序?

    Private Sub ReadExcel(ByVal childform As Fone_Builder_Delux.frmData, ByVal FileName As String)
' In progress
childform.sampleloaded = False
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet

xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Open(FileName)
xlWorkSheet = xlWorkBook.Worksheets(1)
Dim columnrange = xlWorkSheet.Columns
Dim therange = xlWorkSheet.UsedRange


childform.datagridHeaders.Columns.Add("", "") ' Super imporant to add a blank column, could improve this
For cCnt = 1 To therange.Columns.Count

Dim Obj = CType(therange.Cells(1, cCnt), Excel.Range)
childform.datagridSample.Columns.Add(Obj.Value, Obj.Value)
childform.datagridHeaders.Columns.Add(Obj.Value, Obj.Value)

Next

For rCnt = 2 To therange.Rows.Count
Dim rowArray(therange.Columns.Count) As String
For cCnt = 1 To therange.Columns.Count

Dim Obj = CType(therange.Cells(rCnt, cCnt), Excel.Range)
Dim celltext As String
celltext = Obj.Value.ToString
rowArray((cCnt - 1)) = celltext
'MsgBox(Obj.Value)

Next
childform.datagridSample.Rows.Add(rowArray)
Next

AdjustHeaders(childform)
childform.sampleloaded = True
End Sub

最佳答案

简短回答:适当关闭每个项目,然后调用 FinalReleaseComObject在他们身上。

GC.Collect()
GC.WaitForPendingFinalizers()

If xlWorkSheet Is Nothing Then Marshal.FinalReleaseComObject(xlWorkSheet)
If xlWorkBook Is Nothing Then
xlWorkBook.Close(false, false)
Marshal.FinalReleaseComObject(xlWorkBook)
End If
xlApp.Quit()
Marshal.FinalReleaseComObject(xlApp)

长答案:阅读此文answer to another question (整篇文章也很有帮助)。

关于vb.net - 在 vb.net 中读取 Excel 文件会使 Excel 进程挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1610743/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com