一、导出并打开新增的Excel文件,然后再手动保存。
Private Sub Command1_Click() '将查询数据导出到新增的Excel表里
Dim
xlsApp As Excel.Application
'添加工程-引用"Microsoft Excel 11.0 Object Library"才能使用
Dim
xlsBook As Excel.Workbook
Dim
xlsSheet As Excel.Worksheet
Dim i
As Integer
Dim j
As Integer
Set xlsApp = New
Excel.Application
Set xlsBook =
xlsApp.Workbooks.Add
Set xlsSheet =
xlsBook.Worksheets(1)
xlsSheet.Cells(1, 1) = "水库水位监测数据"
With
Form5.MSHFlexGrid1 '或MSFlexGrid1
For i = 0 To .Rows -
1
For j = 1 To 1.Cols - 1
xlsSheet.Cells(i + 2, j) =.TextMatrix(i, j)
Next
Next
End with
xlsApp.Visible = True
'对象可见
Set xlsSheet =
Nothing
Set xlsBook =
Nothing
Set xlsApp =
Nothing
End Sub
二、导出到指定的Excel文件中并保存。
Private Sub Command1_Click() '将查询数据导出到已有的Excel的99.xls表中
Dim xlsApp As
Object
Dim xlsBook As
Object
Dim Fname$, i&,
j&
Set xlsApp =
CreateObject("Excel.Application")
'创建excel对象
xlsApp.Visible =
True
'对象可见
xlsApp.DisplayAlerts =
True
'关闭时提示保存
Set xlsBook =
xlsApp.Workbooks.Open(App.Path & "\99.xls")
'打开文件
If
Val(xlsApp.Application.Version) >= 8 Then
Set xlsBook = xlsApp.ActiveSheet
Else
Set xlsBook = xlsApp
End If
xlsApp.Sheets("Sheet1").Select
For i = 0 To
MSHFlexGrid1.Rows - 1
For j = 0 To MSHFlexGrid1.Cols - 1
xlsApp.Cells(i + 1, j + 1) =
MSHFlexGrid1.TextMatrix(i, j)
Next
Next
xlsBook.SaveAs (App.Path
& "\99.xls")
xlsApp.Quit
Set xlsBook =
Nothing
Set xlsApp =
Nothing
End Sub
加载中,请稍候......