1.引用
2.在模块中编写代码
Public Function ExportToExcel(msflexgrid As msflexgrid)
Dim xlapp As Excel.Application '定义excel类
Dim xlbook As Excel.Workbook '定义工作簿类
Dim xlsheet As Excel.Worksheet '定义工作表类
Dim i As Integer '定义控件的行数
Dim j As Integer '定义控件的列数
Set xlapp = CreateObject("excel.application") '创建excel应用类
xlapp.Visible = True '设置excel对象可见
Set xlbook = xlapp.Workbooks.Add '打开ex1cel工作簿文件
Set xlsheet = xlbook.Worksheets("sheet1") '打开excel工作表
'将数据导入excel表中
'行循环
For i = 0 To msflexgrid.Rows - 1
'列循环
For j = 0 To msflexgrid.Cols - 1
msflexgrid.Row = i
msflexgrid.Col = j
'保存在excel中
xlbook.Worksheets("sheet1").Cells(i + 1, j + 1) = msflexgrid.Text
Next j
Next i
msflexgrid.Redraw = True '允许重画表格控件
End Function
3.在窗体中调用
Private Sub cmdexcel_Click()
Call ExportToExcel(MSFlexGrid1)
End Sub