无法关闭Excel进程
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进程,请问高手怎样解决?
[解决办法]
要从小到大地释放对象
Set xlsReportBook = Nothing
Set xlsReportSheet = Nothing
xlsReportApp.Quit
Set xlsReportApp = Nothing
[解决办法]
仔细查检一下吧!!
我的一个EXCEL格式转换程序里也是这样使用的,没有一点问题.
[解决办法]
在你这个连接失效前,估计EXCEL是不会自己退出的....
[解决办法]
'打开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进程