調用退出后不退出的應用程序嘿,伙計們,我有個小問題,我似乎搞不懂。我正在將DataGridView(它的內容)保存到XLS文件中。我沒有問題這樣做,但在我的任務經理,它仍然顯示,它正在運行。我打電話給: xlApp.Application.Quit()聲明如下: Dim xlApp As New excel.Application這似乎不起作用,但當我允許用戶選擇將其導出到Word文檔時,這也是我退出的方式。我不知道我哪里出錯了.。這是我的完整代碼Imports Word = Microsoft.Office.Interop.Word Imports Excel = Microsoft.Office.Interop.Excel Public Class Form1Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For x As Integer = 1 To 3500 DataGridView1.Rows.Add(New Object() {"r" & x.ToString & "c1", "r" & x.ToString & "c2", "r" & x.ToString & "c3", "r" & x.ToString & "c4", "r" & x.ToString & "c5"}) NextEnd SubPrivate Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click exportToWord (DataGridView1)End SubPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim xlApp As New Excel.Application Dim xlWorkBook As Excel.Workbook Dim xlWorkSheet As Excel.Worksheet 'Dim misValue As Object = System.Reflection.Missing.Value xlWorkBook = xlApp.Workbooks.Add xlWorkSheet = DirectCast(xlWorkBook.Sheets("sheet1"), Excel.Worksheet) xlApp.Visible = True Dim headers = (From ch In DataGridView1.Columns _ Let header = DirectCast(DirectCast(ch, DataGridViewColumn).HeaderCell, DataGridViewColumnHeaderCell) _ Select header.Value).ToArray() Dim headerText() As String = Array.ConvertAll(headers, Function(v) v.ToString) Dim items() = (From r In DataGridView1.Rows _ Let row = DirectCast(r, DataGridViewRow) _ Where Not row.IsNewRow _ Select (From cell In row.Cells _ Let c = DirectCast(cell, DataGridViewCell) _ Select c.Value).ToArray()).ToArray()
3 回答

慕田峪9158850
TA貢獻1794條經驗 獲得超8個贊
“到今天為止,使用COM對象的正確方法是什么?"
指出COM對象引用在調試器下保持活動狀態(tài)。解決方法是從調用COM過程的過程中調用GC。對我起作用了。
最后在一個TRY CATCH塊中運行GC。
抄錄自:post by "Govert" on what is the right way to work with COM objects?
using?System;using?System.Runtime.InteropServices;using?Microsoft.Office.Interop.Excel;namespace?TestCsCom{ ????????Class?Program????{ ????????static?void?Main(string[]?args) ????????{ ????????????//?NOTE:?Don't?call?Excel?objects?in?here...? ????????????//???????Debugger?would?keep?alive?until?end,?preventing?GC?cleanup????????????//?Call?a?separate?function?that?talks?to?Excel ????????????DoTheWork(); ????????????//?Now?let?the?GC?clean?up?(repeat,?until?no?more) ????????????do ????????????{ ????????????????GC.Collect(); ????????????????GC.WaitForPendingFinalizers(); ????????????} ????????????while?(Marshal.AreComObjectsAvailableForCleanup()); ????????} ????????static?void?DoTheWork() ????????{ ????????????Application?app?=?new?Application(); ????????????Workbook?book?=?app.Workbooks.Add(); ????????????Worksheet?worksheet?=?book.Worksheets["Sheet1"]; ????????????app.Visible?=?true; ????????????for?(int?i?=?1;?i?<=?10;?i++)?{ ????????????????worksheet.Cells.Range["A"?+?i].Value?=?"Hello"; ????????????} ????????????book.Save(); ????????????book.Close(); ????????????app.Quit(); ????????????//?NOTE:?No?calls?the?Marshal.ReleaseComObject()?are?ever?needed????????} ????}}

狐的傳說
TA貢獻1804條經驗 獲得超3個贊
Sub ExitWorkBook()Dim wb As WorkbookDim c As Integer c = 0 For Each wb In Application.Workbooks c = c + 1 Next wb If c = 1 Then Application.Quit '--Quit this worksheet but keep excel open. Else Workbooks("(excel workbook name).xls").Close '-- Close Excel End If'End Sub
添加回答
舉報
0/150
提交
取消