一段参考程序:
Option Explicit
Dim EXCAPP As Object '定义一个EXCEL对象
Private Sub CmdCancel_Click()
Unload Me
End
End Sub
Private Sub CmdOutToExcel_Click()
On Error Resume Next
Dim fs
Dim deffile As String, Hfile As String, sfilename As String
Set fs = CreateObject("Scripting.FileSystemObject")
deffile = App.Path & "\abc.xls" '模板文件名称
CommonDialog1.Filter = "*.xls|*.xls"
CommonDialog1.ShowSave
If CommonDialog1.FileName <> "" Then
sfilename = CommonDialog1.FileName
Hfile = Dir(sfilename)
If Hfile = "" Then
fs.CopyFile deffile, sfilename
Else
If MsgBox("此文件已存在!你是否要覆盖此文件?", vbYesNo, "询问") = vbYes Then
fs.CopyFile deffile, sfilename, True
End If
End If
Else
Set fs = Nothing
Exit Sub
End If
Set fs = Nothing
If fill_execl_col = True Then
MsgBox "已将数据成功导入电子表格!"
Else
MsgBox "操作有误!请检查操作是否正确……!"
End If
End Sub
Private Sub Form_Load()
'DataEnvironment1.rsCommand1.Source = ""
End Sub
'填充execl单元
Private Function fill_execl_col(ByVal sfilename As String) As Boolean
On Error GoTo err:
Dim i As Integer
Dim n As Integer
Set EXCAPP = CreateObject("EXCEL.application")
EXCAPP.Visible = False
EXCAPP.Workbooks.Open (sfilename)
For i = 0 To mshflexgrid1.Rows - 1
For n = 0 To mshflexgrid1.Columns - 1
EXCAPP.Worksheets(1).Cells(n, i + 1).Value = mshflexgrid1.Textmatrix(i,n)
Next
Next
EXCAPP.Workbooks(1).Save
EXCAPP.Workbooks(1).Close
EXCAPP.Application.Quit
Set EXCAPP = Nothing
err:
If err.Number <> 0 Then
fill_execl_col = False
EXCAPP.Application.Quit
Set EXCAPP = Nothing
Else
fill_execl_col = True
End If
End Function