The computer room charging system students view the machine record (MSHFlexGrid control data export Excel)

This form can proceed smoothly, relying on the power of the giant, standing on the shoulders of the giant, Excel The export is still very magical, let us take a look at it ^_^

一, students view the flow chart of the machine record form

二, query set

1, machine room charging system How to export

2, vb in DoEvents

三, code fragment

MSHFlexGrid control data export to Excel

Private Sub cmdexportexcel_Click()
    Dim ExcelApp As Excel.Application 'Define an Excel spreadsheet application
    Dim ExcelBook As Excel.Workbook 'Define Excel Form Workbook
    Dim ExcelSheet As Excel.Worksheet 'Define Excel Form Worksheet

    Dim i As Integer 'Define row variables in excel table
    Dim j As Integer 'Define column variables in the excel table
    
    Set ExcelApp = CreateObject("Excel.application") 'Create an excel application object
    Set ExcelBook = ExcelApp.Workbooks.Add 'Create a workbook
    Set ExcelSheet = ExcelBook.Worksheets(1) 'Create a worksheet
    
    DoEvents
    'Because the following code runs for a long time, so transfer control, let the operating system handle other events, avoid the operation is not responding, mistakenly believe that the crash
    
    If myflexgrid.Rows <= 1 Then
        MsgBox "No data to export!", vbOKOnly, "Tips:"
    End If
    
    With myflexgrid
        For i = 0 To .Rows - 1 'Add a line of content in a loop
            For j = 0 To .Cols - 1 'Add a column to the loop
            DoEvents
            ExcelApp.ActiveSheet.Cells(i + 1, j + 1) = .TextMatrix(i, j) 'Add cell content
            Next j
        Next i
    End With
    
    ExcelApp.ActiveWorkbook.SaveAs App.Path & "\Student Query.xls" 'Set Excel Save Path
    ExcelApp.ActiveWorkbook.Saved = True 'Save excel form
    MsgBox "Exported successfully!", vbOKOnly, "Tips:"
    ExcelApp.Visible = True 'display excel form
    
    
    Set ExcelApp = Nothing 'Free ExcelApp object
    Set ExcelBook = Nothing
    Set ExcelSheet = Nothing
End Sub

is the simple combing of this form, the next stop walk ^_^