Private Sub Command3_Click()
Dim exlApp As Excel.Application
Dim exlBook As Excel.Workbook
Dim exlSheet As Excel.Worksheet
Dim sh_index As Integer
Dim num As Long
Dim i, j As Long
Dim rows, cols As Long
On Error Resume Next
Set exlApp = GetObject(, "Excel.Application") 'Excel是否有运行的实例
If Err.Number <> 0 Then
Set exlApp = New Excel.Application
exlApp.Workbooks.Open App.Path + "\rptBookXKCX.xlt"
End If
Err.Clear
On Error GoTo 0
Select Case SSTab1.Tab
Case 0
Adodc1.Refresh
num = Adodc1.Recordset.RecordCount
If num = 0 Then
MsgBox "没有可供输出的数据!", vbOKOnly + 16, "提示"
Exit Sub
End If
rows = 2
For i = 1 To num
exlApp.Sheets(1).Cells(rows, 1) = i
cols = 2
For j = 1 To 5
exlApp.Sheets(1).Cells(rows, cols) = Adodc1.Recordset.Fields(j)
cols = cols + 1
Next j
rows = rows + 1
Adodc1.Recordset.MoveNext
Next i
With exlApp.Sheets(1)
.Range(.Cells(2, 1), .Cells(rows - 1, cols - 1)).Borders.LineStyle = xlContinuous
End With
exlApp.Sheets(1).Activate
exlApp.Visible = True
Case 1
Adodc2.Refresh
num = Adodc2.Recordset.RecordCount
If num = 0 Then
MsgBox "没有可供输出的数据!", vbOKOnly + 16, "提示"
Exit Sub
End If
exlApp.Sheets(2).Cells(1, 3) = Adodc2.Recordset.Fields("课程")
rows = 3
For i = 1 To num
exlApp.Sheets(2).Cells(rows, 1) = i
exlApp.Sheets(2).Cells(rows, 2) = Adodc2.Recordset.Fields("学号")
exlApp.Sheets(2).Cells(rows, 3) = Adodc2.Recordset.Fields("姓名")
exlApp.Sheets(2).Cells(rows, 4) = Adodc2.Recordset.Fields("班级")
exlApp.Sheets(2).Cells(rows, 5) = Adodc2.Recordset.Fields("教师")
rows = rows + 1
Adodc2.Recordset.MoveNext
Next i
With exlApp.Sheets(2)
.Range(.Cells(3, 1), .Cells(rows - 2, 5)).Borders.LineStyle = xlContinuous
End With
exlApp.Sheets(2).Activate
exlApp.Visible = True
Case 2
Adodc3.Refresh
num = Adodc3.Recordset.RecordCount
If num = 0 Then
MsgBox "没有可供输出的数据!", vbOKOnly + 16, "提示"
Exit Sub
End If
exlApp.Sheets(3).Cells(1, 3) = Adodc3.Recordset.Fields("教师")
rows = 3
For i = 1 To num
exlApp.Sheets(3).Cells(rows, 1) = i
exlApp.Sheets(3).Cells(rows, 2) = Adodc3.Recordset.Fields("学号")
exlApp.Sheets(3).Cells(rows, 3) = Adodc3.Recordset.Fields("姓名")
exlApp.Sheets(3).Cells(rows, 4) = Adodc3.Recordset.Fields("班级")
exlApp.Sheets(3).Cells(rows, 5) = Adodc3.Recordset.Fields("课程")
rows = rows + 1
Adodc3.Recordset.MoveNext
Next i
With exlApp.Sheets(3)
.Range(.Cells(3, 1), .Cells(rows - 1, 5)).Borders.LineStyle = xlContinuous
End With
exlApp.Sheets(3).Activate
exlApp.Visible = True
End Select