【機房收費系統】學生充值記錄查詢

學生充值記錄查詢用到了導出EXcel,我一開始看到的時候是懵的,怎樣才能導出excel呢?因爲直接沒有接觸過。

邏輯並不是很難。輸入卡號,查詢導出excel即可。但是本窗體用到了recharge_info表和student_info表

看一下流程圖吧!

窗體很簡單,就不展示了。看一下重要代碼

   '判斷是否輸入卡號
        If Trim(txtcardno.Text = "") Then
            MsgBox "請輸入卡號!", vbOKOnly + vbExclamation, "警告"
            txtcardno.Text = ""
            txtcardno.SetFocus
            Exit Sub
        Else

            '判斷卡號輸入是否爲數字
        If Not IsNumeric(txtcardno.Text) Then
            MsgBox "卡號請輸入數字!", vbOKOnly + vbExclamation, "警告"
             txtcardno.Text = ""
             txtcardno.SetFocus
             Exit Sub
             
        Else
                        '判斷卡號是否註冊
        txtsql = "select * from student_info where cardno='" & txtcardno.Text & "'"
        Set mrc = executeSQL(txtsql, msgtext)
            If mrc.EOF = True Then
                MsgBox "卡號沒有註冊,不存在,請重新輸入卡號!", vbOKOnly + vbExclamation, "警告"
                txtcardno.Text = ""
                txtcardno.SetFocus
                Exit Sub
            End If
        End If
    End If
    txtsql = "select * from recharge_info"
    Set mrc1 = executeSQL(txtsql, msgtext)
    
            With MSHFlexGrid1
            .rows = 1
            .CellAlignment = 4
            .TextMatrix(0, 0) = "學號"
            .TextMatrix(0, 1) = "卡號"
            .TextMatrix(0, 2) = "充值金額"
            .TextMatrix(0, 3) = "充值日期"
            .TextMatrix(0, 4) = "充值時間"
            .TextMatrix(0, 5) = "充值教師"
            Do While mrc1.EOF = False
            .rows = .rows + 1
            .CellAlignment = 4
            .TextMatrix(.rows - 1, 0) = Trim(mrc1.Fields(1))
            .TextMatrix(.rows - 1, 1) = Trim(mrc1.Fields(2))
            .TextMatrix(.rows - 1, 2) = Trim(mrc1.Fields(3))
            .TextMatrix(.rows - 1, 3) = Trim(mrc1.Fields(4))
            .TextMatrix(.rows - 1, 4) = Trim(mrc1.Fields(5))
            .TextMatrix(.rows - 1, 5) = Trim(mrc1.Fields(6))
                mrc1.MoveNext
                Loop
                End With

一個小知識點,會減少代碼量點的呦!

就是把導出excel的代碼寫進模塊中,在窗體中直接調用

On eror GoTo ErrorMsg
    Dim xlApp As Object         '申明Object類對象 後期綁定
    Dim xlBook As Object        '
    Dim rows As Integer         '總行數
    Dim cols As Integer         '總列數
    Dim irow As Integer         '
    Dim hcol As Integer         '
    Dim icol As Integer         '

    If MSFlexGrid1.rows <= 1 Then                            '判斷有無數據
        MsgBox "沒有數據!", vbInformation, "提示"
        Exit Function
    Else
        Set xlApp = CreateObject("Excel.Application")       '生成新的對象引用,引用Excel
        Set xlBook = xlApp.Workbooks.Add                    '創建空白的工作簿
        xlApp.Visible = True                                'Excel可見

        With MSFlexGrid1
            rows = .rows
            cols = .cols
            irow = 0
            icol = 1
            For hcol = 0 To cols - 1                        '列循環
                For irow = 1 To rows                        '行循環
                    xlApp.Cells(irow, icol).Value = .TextMatrix(irow - 1, hcol)     '將表中數據送到Excel
                Next irow
                icol = icol + 1
            Next hcol
        End With

        With xlApp
            .rows(1).Font.Bold = True                       '第一行爲粗體
            .Cells.Select                                   '選擇整個工作表
            '.Columns.AutoFit                                '自動調整列寬以適應文字
'            .Cells(1, 1).Select                             '
        End With

        xlApp.DisplayAlerts = False                         '關閉工作表,不提示用戶保存
        Set xlApp = Nothing                                 '釋放xlApp對象
        Set xlBook = Nothing                                '釋放xlBook對象
        Exit Function
    End If
ErrorMsg:
    MsgBox "當前無法導出爲Excel!", vbOKOnly + vbExclamation, "提示"
End Function

窗體中:

Rem:導出爲EXCEL
Private Sub cmdexcel_Click()
 Call ExportToExcel(MSHFlexGrid1)
End Sub

                     希望可以幫助到大家,有更好的建議評論在下方!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章