'可以先用Excel制作一個Sample.xlt文件,保存到該project的路徑. Private Function ExportToExcel() As Boolean On Error GoTo ErrHandle Dim l_xlsApp As Excel.Application Dim l_xlsWB As Excel.Workbook Dim l_xlsWS As Excel.Worksheet Set l_xlsApp = New Excel.Application Set l_xlsWB = l_xlsApp.Workbooks.Open(App.Path & IIf(Right(App.Path, 1) = "\", "", "\") & "Sample.xlt") Set l_xlsWS = l_xlsWB.Worksheets(1) l_xlsWS.Range("A1").Value = "this is a test" l_xlsWB.SaveAs l_strFileName ExportToExcel = True MsgBox "已將數據成功匯出至'" & l_strFileName & "'", vbInformation, "系統信息" ProcExit: If Not l_xlsWS If Not l_xlsWB Is Nothing Then Set l_xlsWB = Nothing If Not l_xlsApp Is Nothing Then l_xlsApp.Quit Set l_xlsApp = Nothing End If Exit Function ErrHandle: ExportToExcel = False Msgbox Err.Description Err.Clear Resume ProcExit End Function
Private Function ExportToExcel() As Boolean
On Error GoTo ErrHandle
Dim l_xlsApp As Excel.Application
Dim l_xlsWB As Excel.Workbook
Dim l_xlsWS As Excel.Worksheet Set l_xlsApp = New Excel.Application
Set l_xlsWB = l_xlsApp.Workbooks.Open(App.Path & IIf(Right(App.Path, 1) = "\", "", "\") & "Sample.xlt")
Set l_xlsWS = l_xlsWB.Worksheets(1)
l_xlsWS.Range("A1").Value = "this is a test"
l_xlsWB.SaveAs l_strFileName
ExportToExcel = True
MsgBox "已將數據成功匯出至'" & l_strFileName & "'", vbInformation, "系統信息"
ProcExit:
If Not l_xlsWS
If Not l_xlsWB Is Nothing Then Set l_xlsWB = Nothing
If Not l_xlsApp Is Nothing Then
l_xlsApp.Quit
Set l_xlsApp = Nothing
End If
Exit Function
ErrHandle:
ExportToExcel = False
Msgbox Err.Description
Err.Clear
Resume ProcExit
End Function