czxzx2014
2017-09-26 08:48
采纳率: 15.4%
浏览 2.0k

vb.net调用Excel后怎么关闭Excel进程

下面这样一段代码,创建一个Excel文件,写入数据,再关闭,但是每执行一次,电脑就增加了一个Excel进程,请大神们解答怎么才能关闭Excel进程,试过代码中的方法,不行

Sub creatExcel(number As String)
    Dim ApplicationPath As String
    Dim SaveFileName As String
    ApplicationPath = Application.StartupPath & "\Data\"
    ApplicationPath = ApplicationPath & Now.ToString("yyy") & "\" & Now.ToString("MMdd")
    If Directory.Exists(ApplicationPath) = False Then       '判断是否存在该路径,如果不存在则创建路径
        Directory.CreateDirectory(ApplicationPath)
    End If

    SaveFileName = ApplicationPath & "\" & number & ".xlsx"


    Dim Excelobjict As New Microsoft.Office.Interop.Excel.Application
    Excelobjict.Visible = False  '可见
    Excelobjict.Workbooks.Add()



    Excelobjict.Columns(1).columnwidth = 100
    Excelobjict.Columns(2).columnwidth = 30
    Excelobjict.Columns(1).font.size = 28
    Excelobjict.Columns(2).font.size = 28

    Excelobjict.Range("A1").Select()
    Excelobjict.ActiveCell.FormulaR1C1 = "d1111"
    Excelobjict.Range("A2").Select()
    Excelobjict.ActiveCell.FormulaR1C1 = "xxx"
    Excelobjict.Range("A3").Select()
    Excelobjict.ActiveCell.FormulaR1C1 = "3"

    Excelobjict.ActiveWorkbook.SaveAs(SaveFileName)
    Excelobjict.ActiveWorkbook.Close()
    System.Runtime.InteropServices.Marshal.ReleaseComObject(Excelobjict)
    'Excelobjict.Quit()
    Excelobjict = Nothing
    GC.Collect()
End Sub

1条回答 默认 最新

相关推荐 更多相似问题