VB之旅-调用Excel导出控件MSHFlexGrid的数据

前端之家收集整理的这篇文章主要介绍了VB之旅-调用Excel导出控件MSHFlexGrid的数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<strong><span style="font-size:18px;">Public Sub ExportToExcel(FormName As Form,FlexgridName As String) '导出Excel表的过程
    Dim xlApp As Object 'Excel程序
    Dim xlBook As Object  'Excel工作薄
    Dim xlSheet As Object  'Excel工作表


    Screen.MousePointer = vbHourglass '鼠标指示为等待状态,根据电脑不同可能为沙漏,也可能为转圈
    On Error GoTo Err_Proc
    Set xlApp = CreateObject("Excel.Application") '调用execel程序
    Set xlBook = xlApp.Workbooks.Add '添加工作薄
    Set xlSheet = xlBook.Worksheets(1) '添加工作单元


    '向工作单元中添加数据
    Dim i As Long
    Dim j As Integer
    
    With FormName.Controls(FlexgridName)
        For i = 0 To .Rows - 1 '行依次增加 从0开始    外循环,外循环每增加1,内循环循环一周
            For j = 0 To .Cols - 1 '一行有.cols列 从0开始   内循环
                xlSheet.Cells(i + 1,j + 1).Value = "'" & .TextMatrix(i,j) '工作表中采用(x,y)坐标,都从1开始
            Next j
        Next i
    End With
    xlApp.Visible = True '设定Excel程序可见
    Screen.MousePointer = vbDefault '鼠标指针设为默认状态
    Exit Sub
Err_Proc:
    Screen.MousePointer = vbDefault '鼠标指针设为默认状态
    MsgBox "请确认您的电脑已安装Excel,或是否安装正确!",vbExclamation,"机房收费系统"
End Sub
然后在每个导出Excel命令中添加代码
Private Sub cmdExcel_Click()
    Call ExportToExcel(Me,"FlexGridSt")
End Sub</span></strong>

猜你在找的VB相关文章