我写的一个把表格中的数据导出到execl中的函数,能导出成功,但就是导出一次后,就要关闭一次应用程序,然后再次进入程序才可以再导出到execl
suinx 2003-08-22 09:24:15 我写的一个把表格中的数据导出到execl中的函数,能导出成功,但就是导出一次后,就要关闭一次应用程序,然后再次进入程序才可以再导出到execl,否则就导不出来.我想是对象没有完全释放的原因?
那位有这方面的经验,请指点一下,谢谢!
代码如下:
Private Function GridSaveAsExcel2(vsGrid As VSFlexGrid, Optional strFile As String, Optional strTitle As String) As Boolean
On Error GoTo errHandle
Dim i As Integer, j As Integer
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook 'Object
Dim xlSheet As Excel.Worksheet ' Object
Set xlApp = New Excel.Application
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks().Add
Set xlSheet = xlBook.Worksheets("sheet1")
xlSheet.Cells(1, 1).Value = strTitle
'跨列居中
Range("A1:G1").Select
With Selection
.HorizontalAlignment = xlCenter
' .VerticalAlignment = xlBottom
' .WrapText = False
' .Orientation = 0
' .AddIndent = False
' .ShrinkToFit = False
.MergeCells = True
End With
Selection.Merge
With vsGrid
For i = 0 To .Rows - 1
For j = 0 To .Cols - 1
If .ColFormat(j) = "" Then
If Len(Trim(.TextMatrix(i, j))) >= 8 And j < 3 Then
xlSheet.Cells(i + 2, j + 1) = "'" & Trim(.TextMatrix(i, j))
Else
xlSheet.Cells(i + 2, j + 1) = Trim(.TextMatrix(i, j))
End If
Else
xlSheet.Cells(i + 2, j + 1) = Trim(.TextMatrix(i, j))
End If
Next j
Next i
End With
ActiveWorkbook.SaveAs FileName:=strFile, FileFormat:=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
' 关闭工作表。
xlBook.Close
'用 Quit 方法关闭 Microsoft Excel
xlApp.Quit
'释放对象
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
GridSaveAsExcel2 = True
Exit Function
errHandle:
GridSaveAsExcel2 = False
End Function