VB将MSHFlexGrid数据导出到Excel文件通用功能

 1.通用导出Excel功能。

 2.将 MSHFlexGrid数据导出到Excel文件通用功能。

 3.具体代码如下:

 

'将下列代码保存到一模块文件中,调用方法:Export fgrid1,cd1



Public Sub Export(flexgridname As MSHFlexGrid, CD1 As CommonDialog)

Dim xlApp As Object 'Excel.Application

Dim xlBook As Object  'Excel.Workbook

Dim xlSheet As Object  'Excel.Worksheet



    Screen.MousePointer = vbHourglass

    On Error GoTo Err_Proc

    Set xlApp = CreateObject("Excel.Application")

    Set xlBook = xlApp.Workbooks.Add

    Set xlSheet = xlBook.Worksheets(1)



    'Begin to fill data to sheet

    Dim i As Long

    Dim j As Integer

'    With formname.Controls("flexgridname")

        For i = 0 To flexgridname.rows - 1

            For j = 0 To flexgridname.Cols - 1

                xlSheet.Cells(i + 1, j + 1).Value = "'" & flexgridname.TextMatrix(i, j)

            Next j

        Next i

'     End With

     xlApp.Visible = True

     Screen.MousePointer = vbDefault

     Exit Sub

Err_Proc:

    Screen.MousePointer = vbDefault

    '报错以后用老的导出函数

    savef flexgridname, CD1

'    'MsgBox "请确认您的电脑已安装Excel!", vbExclamation, "提示"

    

End Sub

 

你可能感兴趣的:(Excel)