将MSHFlexGrid1中记录导出为Excel

时间:2021-07-08 23:59:38

1、添加引用Microsoft Excel 14.0 Object Library

将MSHFlexGrid1中记录导出为Excel

2、编写代码部分

Private Sub Output_Click()
Dim i As Integer '定义变量
Dim j As Integer
Dim Excelapp As Excel.Application
Dim Excelbook As Excel.Workbook
Dim Excelsheet As Excel.Worksheet Set Excelapp = New Excel.Application '建立一张表
Set Excelbook = Excelapp.Workbooks.Add '添加新工作薄
Set Excelsheet = Excelbook.Worksheets(1) '建立工作表 DoEvents ' DoEvents函数转让控制权,以便让操作系统处理其它的事件。 For i = 0 To myflexgrid.Rows - 1 '将myflexgrid中内容写到表格中
For j = 0 To myflexgrid.Cols - 1 DoEvents
Excelapp.ActiveSheet.Cells(i + 1, j + 1) = myflexgrid.TextMatrix(i, j)
Next j
Next i
End With
Excelapp.ActiveWorkbook.SaveAs "E:\机房收费总文件\充值.xls" '设置表格保存路径
Excelapp.ActiveWorkbook.Saved = True '保存表格
Excelapp.Quit
MsgBox "充值记录导出完成!", vbInformation, "提示"
End Sub