Set xlsReportApp = CreateObject("excel.application") 
Set xlsReportBook = xlsReportApp.Workbooks.Open("C\A.xls")    
Set xlsReportSheet = xlsReportBook.Worksheets("Report")    If xlsCnnPP.State <> adStateClosed Then
        xlsCnnPP.Close
    End If
'创建同个EXCEL的连接对象
    With xlsCnnPP
        .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\A.xls;Extended Properties=""Excel 8.0;"";"
        .CommandTimeout = 5
        .Open
    End With
    
    xlsCnnPP.Close
    Set xlsCnnPP = Nothing    xlsReportBook.Close (True)
    xlsReportApp.Quit
    Set xlsReportSheet = Nothing
    Set xlsReportBook = Nothing
    Set xlsReportApp = Nothing   
 '无法关闭excel进程,如不创建xlscnnpp连接对象,可以正常释放excel进程,请问高手怎样解决?

解决方案 »

  1.   

    要从小到大地释放对象
        Set xlsReportBook = Nothing
        Set xlsReportSheet = Nothing
        xlsReportApp.Quit
        Set xlsReportApp = Nothing   
      

  2.   

    仔细查检一下吧!!我的一个EXCEL格式转换程序里也是这样使用的,没有一点问题.
      

  3.   

    如果我不加下面语句我也正正常关闭EXCEL进程    If xlsCnnPP.State <> adStateClosed Then
            xlsCnnPP.Close
        End If
    '创建同个EXCEL的连接对象
        With xlsCnnPP
            .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\A.xls;Extended Properties=""Excel 8.0;"";"
            .CommandTimeout = 5
            .Open
        End With
      

  4.   

    在你这个连接失效前,估计EXCEL是不会自己退出的....
      

  5.   

    如果仅仅为了通过 ODBC 访问 .xls 不需要用 excel.application 打开文件,ODBC 自己会处理的。
      

  6.   

    '打开excel文件
    Public Sub openExcel(ByVal strName As String)
    If flag Then
        closeExcel
    End If
    Set xlsApp = CreateObject("Excel.Application")
    Set xlsBook = xlsApp.workbooks.Open(strName)
    Set xlsSheet = xlsBook.ActiveSheet
    flag = True
    End Sub'关闭excel文件
    Public Sub closeExcel()
    If flag Then
        flag = False
        xlsBook.Close
        xlsApp.Quit
        Set xlsApp = Nothing
        Set xlsBook = Nothing
        Set xlsSheet = Nothing
    End If
    End Sub'取得excel文件的记录集
    Public Function GetExcelRs(ByVal strName As String) As ADODB.Recordset
    Dim Rs As ADODB.Recordset
    Set Rs = New ADODB.Recordset
    Dim conn As String
    Rs.CursorLocation = adUseClient
    Rs.CursorType = adOpenDynamic
    Rs.LockType = adLockBatchOptimistic
    conn = "data provider=msdasql.1;driver=microsoft excel driver (*.xls);dbq=" & strName
    Rs.Open "SELECT * FROM [sheet1$]", conn
    Set GetExcelRs = Rs
    Set Rs = Nothing
    End Function楼主
    以上代码是三个子程序
    若要getexcelrs则必须要先closeexcel
    若openexcel后getexcelrs,那么再close的话,rs会占用一个excel进程
    而这个excel进程就没有办法关闭了
    也就是说打开文件和获取recordset不能交叉使用
    也没必要交叉使用
    我一般是通过打开excel来检查里面的固定单元格来检查这个excel文件是否可以正常读取(比如若是导入程序,则只能导入固定格式的)
    检查完毕后关闭excel(进程也就关掉了)
    再利用getexcelrs来获取recordset来读excel的内容
    这样速度就会快很多,而不用循环excel了
    并且干净利落,不会留下残余的excel进程
      

  7.   

    忘了一点,上面的函数
    要创建几个模块级的变量
    dim flag as boolean '标记excel状态
    dim xlsapp as object
    dim xlsbook as object
    dim xlssheet as object
    由于是采用的createobject的方式创建的对象
    所以不必添加ado的引用
      

  8.   

    晕,说错了,由于是采用的createobject的方式创建的对象
    所以不必添加ado的引用应该是所以不必引用EXCEL
      

  9.   

    不如调用API直接把进程杀死吧。(工作时间,无聊乱说,请无视我)
      

  10.   

    非常感谢datou985(^\会急转弯儿的猪/^) ,我知道了,那我只好重新该下程序了。
      

  11.   

    Dim t As Integer = 0
    If xlsBook IsNot Nothing Then
    Runtime.InteropServices.Marshal.ReleaseComObject(xlsBook)
    xlsBook = Nothing
    End If
    If xlsapp IsNot Nothing Then
    t = xlsapp.Hwnd
    Runtime.InteropServices.Marshal.ReleaseComObject(xlsapp)
    xlsapp = Nothing
    End IfGC.Collect(2, GCCollectionMode.Forced)
    GC.WaitForPendingFinalizers()If t > 0 Then
    PostMessage(t, &H10, 0, 0) '关闭EXCEL窗体
    PostMessage(t, &H12, 0, 0) '退出EXCEL窗体
    PostMessage(t, &H2, 0, 0) '摧毁EXCEL窗体
    End If