调用退出后不退出的应用程序嘿,伙计们,我有个小问题,我似乎搞不懂。我正在将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 回答
qq_笑_17
TA贡献1818条经验 获得超7个赞
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贡献1772条经验 获得超8个赞
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
提交
取消
