阳台储物架图片大全:vb MSFlexGrid导出到excel

来源:百度文库 编辑:中财网 时间:2024/05/05 15:24:49
Private Sub ToExcel()            '本函数用于导出商家列表信息
On Error GoTo ddd
If Trim(Text1.Text) = "" Then
      Exit Sub
End If
If Right(Trim(Text1.Text), 4) <> ".xls" Then
   Label1.Caption = "非法的目标文件名。"
   Exit Sub
End If
If Dir(Trim(Text1.Text)) <> "" Then
   MsgBox "设定的目标文件 Excel 文件已经存在,不得使用已经存在的文件的文件名。", vbInformation
   Label9.Caption = "准备就绪。"
   Exit Sub
End If
Label1.Caption = "正在初始化 Excel 后台工作环境。"
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")             '创建EXCEL对象
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
xlSheet.Cells(1, 1) = "写入数据的程序的版本号:"
xlSheet.Cells(1, 2) = App.Major & "." & App.Minor & "." & App.Revision & "。 本软件更新速度较快,请及时更新最新版本。"
xlSheet.Cells(2, 1) = "导出的内容:"
xlSheet.Cells(2, 2) = "商家列表"
xlSheet.Cells(3, 1) = "数据的查询条件:"
' xlSheet.Cells(3, 2) = Label7.Caption
xlSheet.Cells(4, 1) = "导出时间:"
xlSheet.Cells(4, 2) = Now()
xlSheet.Cells(5, 1) = "导出的资料条数:"
'xlSheet.Cells(5, 2) = Label6.Caption
xlSheet.Cells.EntireColumn.AutoFit               '自动调整列宽
Set xlSheet = xlBook.Worksheets(2)
Dim i As Long
Dim t As Long
t = Form2.MSFlexGrid1.Cols
Dim d As Long
d = Form2.MSFlexGrid1.Rows
Dim f As Long
For f = 1 To d
        For i = 1 To t
               xlSheet.Cells(f, i) = Form2.MSFlexGrid1.TextMatrix(f - 1, i - 1)
               DoEvents
        Next i
        DoEvents
Next f
xlSheet.Cells.EntireColumn.AutoFit        '自动调整列宽
Label1.Caption = "数据写入完毕,正在保存文件!"
xlApp.ActiveWorkbook.SaveAs Trim(Text1.Text)
xlBook.Close (True)                 '关闭工作簿
xlApp.Quit                                 '结束EXCEL对象Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing              '释放xlApp对象
Exit Sub
ddd:
Label1.Caption = "错误代码:" & Err.Number & "," & Err.Description
End Sub