在学习查看上机记录的时候,遇到了一些问题,现在都整理好了,想和大家分享一下。
这一块是导出excel表格的部分,大家肯定也总结了不少这部分的知识点,下面话不多说,请看代码。
If txtCardNo.Text = "" Then
MsgBox "卡号不能为空", vbOKOnly + vbExclamation, "警告"
txtCardNo.SetFocus
Else
If Not IsNumeric(Trim(txtCardNo.Text)) Then
MsgBox "请输入数字", vbOKOnly + vbExclamation, "警告"
txtCardNo.Text = ""
txtCardNo.SetFocus
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
Else
txtSQL = "select * from Line_Info where cardno='" & Trim(txtCardNo.Text) & "'"
Set mrc = ExecuteSQL(txtSQL, MsgText)
If mrc.EOF = False Then
With MSHFlexGrid1
Do While mrc.EOF = False
.CellAlignment = 4
.TextMatrix(.Rows - 1, 0) = mrc.Fields(1)
.TextMatrix(.Rows - 1, 1) = mrc.Fields(3)
.TextMatrix(.Rows - 1, 2) = mrc!ondate
.TextMatrix(.Rows - 1, 3) = mrc!OnTime
.TextMatrix(.Rows - 1, 4) = mrc!offdate
.TextMatrix(.Rows - 1, 5) = mrc!offtime
.TextMatrix(.Rows - 1, 6) = mrc!consume
.TextMatrix(.Rows - 1, 7) = mrc!cash
mrc.MoveNext
Loop
End With
上面部分是未导出之前的查看记录显示代码。
Dim xlApp As New Excel.Application '声明Excel对象
Dim xlBook As Excel.Workbook '声明工作簿对象
Dim xlSheet As Excel.Worksheet '声明工作表单
Dim i As Integer
Dim j As Integer
If MSHFlexGrid1.Text = "" Then '判断是否有记录可以导出
MsgBox "没有记录可导出!", vbOKOnly + vbExclamation, "警告"
Exit Sub
Else
Set xlApp = CreateObject("excel.application") '调用execel程序
Set xlBook = xlApp.Workbooks.Add(1) '创建新的空白簿
Set xlSheet = Excel.ActiveWorkbook.ActiveSheet '创建新的工作表单
For i = 0 To MSHFlexGrid1.Rows - 1 '填入数据
For j = 0 To MSHFlexGrid1.Cols - 1
xlSheet.Cells(i + 1, j + 1) = MSHFlexGrid1.TextMatrix(i, j) 'cell(a,b)表示a行,b列
Next j
Next i
xlApp.Visible = True '显示Excel表格
Set xlApp = Nothing '交还控制给Excel
End If