cool hit counter Server Room Charging System - Exporting Data from MSHFlexGrid Control to Excel_Intefrankly

Server Room Charging System - Exporting Data from MSHFlexGrid Control to Excel


Copyright: This is an original post by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/huyuyang6688/article/details/12176225

In the server room charging system, many query forms contain the same function: displaying the data queried in the database in the MSHFlexGrid control, and then exporting the data in the MSHFlexGrid control to an Excel table.

Although I have done student information management system before, I did not involve in this function, so I record it here, for myself, to review and reflect, and for everyone, to share and learn.

Method 1: Create an empty Excel sheet in the root directory beforehand

1. Create an Excel table of the data to be imported in the same root directory as the VB project.

2. Write code in the VB event.

Private Sub cmdExport_Click()
    Dim i As Integer
    Dim j As Integer
    myFlexGrid.Redraw = False    'Close the form to redraw, Speeds up operation
     Set xlApp = CreateObject("Excel.Application")   ' establishEXCEL targets
     ' Open the already existingEXCEL Workbook files
     Set xlBook = xlApp.Workbooks.Open(App.Path & " Student logs on the computer.xls")
    xlApp.Visible = True ' set upEXCEL Visible objects
     Set xlSheet = xlBook.Worksheets("Sheet1") ' Setting up the activity worksheet

     For i = 0 To myFlexGrid.Rows - 1 ' row of cycles
            For j = 0 To myFlexGrid.Cols - 1 ' row of cycles
                 myFlexGrid.Row = i
               myFlexGrid.Col = j
              ' Save toEXCEL									              	     	  	     xlBook.Worksheets("Sheet1").Cells(i + 1, j + 1) = myFlexGrid.Text          					    	 Next j
    Next i
    myFlexGrid.Redraw = True
End Sub

Method 2: Direct reference in VB comes with the Microsoft Excel 14.0 Object Library

1, in the VB Designer, Project → Reference, reference Microsoft Excel 14.0 Object Library.

2. Writing code :

Private Sub cmdExport_Click()
    Dim i As Integer
    Dim j As Integer
    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet
    Set xlApp = CreateObject("Excel.Application")   ' instantiated objectxlApp
    xlApp.Visible = True
    Set xlBook = xlApp.Workbooks.Add
    Set xlSheet = xlBook.Worksheets(1)
    For i = 0 To myFlexGrid.Rows - 1
        For j = 0 To myFlexGrid.Cols - 1
            myFlexGrid.Row = i
            myFlexGrid.Col = j
            xlSheet.Cells(i + 1, j + 1) = Trim(myFlexGrid.Text)
        Next
    Next
End Sub

The first method does not need to refer to Microsoft Excel 14.0 Object Library, but must be established in the root directory of the corresponding Excel table, the second method does not need to establish a table, directly refer to Microsoft Excel 14.0 Object Library can be directly instantiated, more convenient.


Recommended>>
1、zxing based QR code scanning
2、Indepth Weex series ix Summary of WeexSDK learnable details
3、Recommended php coding specification
4、windows setup local domain resolution
5、Xiaolai Investment Walkthrough applet version 09 released

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号