MSHFlexGrid中数据导入到Excel(机房问题)

在机房收费系统中,有的窗体含有导入到Excel功能,比如收取金额查询窗体,我们如何用VB代码实现从MSHFlexGrid中导入数据到Excel呢?

我知道的有两种方法,但是第一种方法需要引用部件,很多电脑上都没有该部件,所以我用了代码方法来实现这个功能。

首先,在模块中:

    Public Sub Export(frmName As Form, FlexGridName As String)

    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)

    '向表中添加数据
    Dim intRowIndex As Integer
    Dim intColIndex As Integer
    
    With frmName.Controls(FlexGridName)  '查找控件
        '填充数据到Sheet1
        For intRowIndex = 0 To .Rows - 1
            For intColIndex = 0 To .Cols - 1
                xlSheet.Cells(intRowIndex + 1, intColIndex + 1).Value = "'" & .TextMatrix(intRowIndex, intColIndex)
            Next intColIndex
        Next intRowIndex
    End With
    
    xlApp.Visible = True
    Screen.MousePointer = vbDefault
    Exit Sub
    
Err_Proc:
    Screen.MousePointer = vbDefault
    MsgBox "请确认您的电脑已安装Excel!", vbExclamation, "提示"
End Sub
然后,在导入CMD下写:

    Export Me, "MSHFlexGrid1"
到此,成功将MSHFlexGrid中数据导入到Excel中。


你可能感兴趣的:(MSHFlexGrid中数据导入到Excel(机房问题))