Dim xlsApp As EXCEL.Application Dim xlsWorkBook As EXCEL.Workbook Dim xlsWorksheet As EXCEL.WorksheetSet xlsApp = CreateObject("Excel.Application") Set xlsWorkBook = xlsApp.Workbooks.Add Set xlsWorksheet = xlsWorkBook.Worksheets(1)关闭时 Set xlsWorkBook = Nothing Set xlsWorksheet = Nothing xlsApp.Quit Set xlsApp = Nothing Set xlsheet = Nothing
上面最后一行去掉 如果想直接杀死进程里的EXCEL用下面这种Public Sub subKillProcess(ByVal strProcess As String) Dim strComputer As String Dim objWMIService As Object Dim colProcessList Dim objProcess As Object
On Error Resume Next
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") ' strProcess = "Excel.exe" Set colProcessList = objWMIService.ExecQuery _ ("Select * from Win32_Process Where Name = '" & strProcess & "'") For Each objProcess In colProcessList objProcess.Terminate Next
1.在導入excel數據時,盡量改用ado的方式打開,速度和效率會更快 Set mConn = New ADODB.Connection With mConn .CursorLocation = adUseClient .Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mPath & "\yourexecl.xls;Extended Properties=""Excel 8.0;""" End With Set mRsRead = New ADODB.Recordset mRsRead.CursorLocation = adUseClient
mRsRead.Open "select F1,F2,F3,F4 from [yourexeclsheet$] ", mConn, adOpenStatic, adLockOptimistic, adCmdText 2.在建立一個APP后,可以根據Workbooks(i).FullName來判斷是哪一個excel 3.關閉excel的順序如下: Set ExcelSheet = Nothing BookWork.Close Set BookWork= Nothing Set ExcelApp= Nothing
直接调用即可
Dim xlsWorkBook As EXCEL.Workbook
Dim xlsWorksheet As EXCEL.WorksheetSet xlsApp = CreateObject("Excel.Application")
Set xlsWorkBook = xlsApp.Workbooks.Add
Set xlsWorksheet = xlsWorkBook.Worksheets(1)关闭时
Set xlsWorkBook = Nothing
Set xlsWorksheet = Nothing
xlsApp.Quit
Set xlsApp = Nothing
Set xlsheet = Nothing
如果想直接杀死进程里的EXCEL用下面这种Public Sub subKillProcess(ByVal strProcess As String) Dim strComputer As String
Dim objWMIService As Object
Dim colProcessList
Dim objProcess As Object
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
' strProcess = "Excel.exe"
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '" & strProcess & "'")
For Each objProcess In colProcessList
objProcess.Terminate
Next
End Sub
'调用
Call subKillProcess("Excel.exe")
Set mConn = New ADODB.Connection
With mConn
.CursorLocation = adUseClient
.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mPath & "\yourexecl.xls;Extended Properties=""Excel 8.0;"""
End With
Set mRsRead = New ADODB.Recordset
mRsRead.CursorLocation = adUseClient
mRsRead.Open "select F1,F2,F3,F4 from [yourexeclsheet$] ", mConn, adOpenStatic, adLockOptimistic, adCmdText
2.在建立一個APP后,可以根據Workbooks(i).FullName來判斷是哪一個excel
3.關閉excel的順序如下:
Set ExcelSheet = Nothing
BookWork.Close
Set BookWork= Nothing
Set ExcelApp= Nothing
要比用excel 程序快的多