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 = 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进程,请问高手怎样解决?
解决方案 »
- 求西门子手机AT指令,有相关的代码更好!
- 用API函数画扇形并填充颜色
- 使用winsocket的senddata方法如果发送失败如何判断?
- 再请问下个关于读写.ini文件问题(主要是删除某句)(50分)
- 怎么样实现与按键精灵相同功能
- 如何设置焦点到指定窗体?用SetFocus吗
- 数据库如何利用VB来导入导出到TXT文件(格式为自定义的)最快最好,请指教?
- 如何在程序中删除数据库?
- wpf 里面Xilium.CefGlue 总是报出中断点这个问题,大家帮忙看一下
- 谁做过药房管理得程序
- 求助:利用printer.paintpicture如何打印出数据表中的图片记录?
- 转换问题(怎样把小写的数字转换成大写的数字??)
Set xlsReportBook = Nothing
Set xlsReportSheet = Nothing
xlsReportApp.Quit
Set xlsReportApp = Nothing
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
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进程
要创建几个模块级的变量
dim flag as boolean '标记excel状态
dim xlsapp as object
dim xlsbook as object
dim xlssheet as object
由于是采用的createobject的方式创建的对象
所以不必添加ado的引用
所以不必添加ado的引用应该是所以不必引用EXCEL
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