调用退出后不退出的应用程序

调用退出后不退出的应用程序

嘿,伙计们,我有个小问题,我似乎搞不懂。我正在将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()


精慕HU
浏览 552回答 3
3回答

慕田峪9158850

“到今天为止,使用COM对象的正确方法是什么?"指出COM对象引用在调试器下保持活动状态。解决方法是从调用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        }     }}

狐的传说

我曾多次使用此功能关闭脚本中的excel文档,同时隐藏、显示,现在关闭,如果它是唯一打开的工作簿,否则请关闭此工作表。这是我的Sub&nbsp;ExitWorkBook()Dim&nbsp;wb&nbsp;As&nbsp;WorkbookDim&nbsp;c&nbsp;As&nbsp;Integer &nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;=&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;For&nbsp;Each&nbsp;wb&nbsp;In&nbsp;Application.Workbooks &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;=&nbsp;c&nbsp;+&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;Next&nbsp;wb&nbsp;&nbsp;&nbsp;&nbsp;If&nbsp;c&nbsp;=&nbsp;1&nbsp;Then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Application.Quit&nbsp;&nbsp;&nbsp;'--Quit&nbsp;this&nbsp;worksheet&nbsp;but&nbsp;keep&nbsp;excel&nbsp;open. &nbsp;&nbsp;&nbsp;&nbsp;Else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Workbooks("(excel&nbsp;workbook&nbsp;name).xls").Close&nbsp;&nbsp;&nbsp;&nbsp;'--&nbsp;Close&nbsp;Excel &nbsp;&nbsp;&nbsp;&nbsp;End&nbsp;If'End&nbsp;Sub
打开App,查看更多内容
随时随地看视频慕课网APP