VB打印表格的问题
本人初学者,用VB写了一个表格,想填写完表格后将它存入Excel数据库然后打印出来,我用打印报表,但是无法连接Excel数据库,只能连接Access数据库,谁可以教教我,谢谢。
Dim i As Integer, r As Integer, c As Integer
Dim newxls As Excel.Application
Dim newbook As Excel.Workbook
Dim newsheet As Excel.Worksheet
Set newxls = CreateObject("Excel.Application")
Set newbook = newxls.Workbooks.Add
Set newsheet = newbook.Worksheets(1)
If sql <> "" Then
Adodc1.RecordSource = sql
Adodc1.Refresh
End If
If Adodc1.Recordset.RecordCount > 0 Then
For i = 0 To DataGrid1.Columns.Count - 1
newsheet.Cells(1, i + 1) = DataGrid1.Columns(i).Caption
Next i
Adodc1.Recordset.MoveFirst
Do Until Adodc1.Recordset.EOF
r = Adodc1.Recordset.AbsolutePosition
For c = 0 To DataGrid1.Columns.Count - 1
DataGrid1.Col = c
newsheet.Cells(r + 1, c + 1) = DataGrid1.Columns(c)
Next c
Adodc1.Recordset.MoveNext
Loop
Dim myval As Long
Dim mystr As String
myval = MsgBox("是否保存EXCEL表", vbYesNo, "提示")
If myval = vbYes Then
mystr = InputBox("请输入文件名称", "提示")
If Len(mystr) = 0 Then
MsgBox "不允许文件名为空", , "提示"
Exit Sub
End If
On Error GoTo ErrSave
newsheet.SaveAs "d:\" & mystr & ".xls"
MsgBox "Excel保存成功,位置D:\" & mystr & ".xls", , "提示"
newxls.Quit
ErrSave:
Exit Sub
MsgBox Err.Description, , "提示"
End If
End If