Print Datagrid Using Datagrid and Adodc Controls http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=31485&lngWId=1
还是导出到Excel是最简单的方法,其他的方法你可以慢慢研究
用ADO对象连接SQL Server 定义cn为ADO.connnection 定义rs为ADO.recordset . . '查询记录 . . set datagrid1.datasoure=rs set datareport1.datasource=rs datasource.show
怎么导出到excel啊,我试了很久都没有调成功,不知道哪的问题??
给你一段程序.是针对MSHFLEXGRID的.你参考一下.. '_______________________________________________ Sub PrintA(PS As Long, PE As Long) '打印成绩表 Dim a As Long Dim B As Long Dim GColWidth As Long Dim GRowHighe As Long Dim LineX As Long Dim LineY As Long Dim DltP As Long Dim PrintR As Long Dim FixLab As String Dim TmpWidth As Long Dim DltH As Long Dim PageStar As Long Dim PageEnd As Long '------------------------------------------------ ProwStar = 6: PageWidth = 0 PrintGrid.Cols = PColMax - 1
LineX = LineX + TmpWidth If a = 0 And B = PrintGrid.Cols - 1 Then Printer.Line (LineX, GridTop)-(LineX, GridTop + PageHeight) End If Next B LineY = LineY + RowHei '画最后一条线 If a = PrintGrid.Rows - 1 Then Printer.Line (GridLeft, LineY)-(GridLeft + PageWidth, LineY) End If Next a Label1 = "第" & PageStar & "页" If GridLeft + PrintGrid.Width > PrintPic.Width - 1200 Then Label1.Left = PrintPic.Width - 1200 Else Label1.Left = GridLeft + PrintGrid.Width - Label1.Width End If Printer.CurrentX = Label1.Left Printer.CurrentY = LineY + 210 Printer.Print Label1 DoEvents If PageStar < PE Then Printer.NewPage Else Printer.EndDoc End If Next PageStar End Sub
2.做报表,把记录集推到报表里面去。
定义cn为ADO.connnection
定义rs为ADO.recordset
.
.
'查询记录
.
.
set datagrid1.datasoure=rs
set datareport1.datasource=rs
datasource.show
'_______________________________________________
Sub PrintA(PS As Long, PE As Long) '打印成绩表
Dim a As Long
Dim B As Long
Dim GColWidth As Long
Dim GRowHighe As Long
Dim LineX As Long
Dim LineY As Long
Dim DltP As Long
Dim PrintR As Long
Dim FixLab As String
Dim TmpWidth As Long
Dim DltH As Long
Dim PageStar As Long
Dim PageEnd As Long
'------------------------------------------------
ProwStar = 6: PageWidth = 0
PrintGrid.Cols = PColMax - 1
PrintGrid.RowHeight(-1) = RowHei
PrintGrid.Font.Name = MainFrm.MainGrid.Font.Name
PrintGrid.Font.Size = MainFrm.MainGrid.Font.Size
GridLeft = PrintGrid.Left - 0.5 * 567: GridTop = PrintGrid.Top
DltP = PRowMax - 1
For PageStar = PS To PE
PrintR = (PRowMax - 1) * (PageStar - 1)
For a = 1 To PrintGrid.Rows - 1
For B = 0 To PColMax - 2
If (PrintR + a + 5) < MainFrm.MainGrid.Rows Then
PrintGrid.TextMatrix(a, B) = MainFrm.MainGrid.TextMatrix(PrintR + a + 5, B + 1)
Else
PrintGrid.TextMatrix(a, B) = ""
End If
Next B
Next a
'开始打印
Printer.Font.Name = PrintGrid.Font.Name
Printer.Font.Size = PrintGrid.Font.Size
Printer.ScaleTop = 0: Printer.ScaleLeft = 0
Printer.ScaleWidth = Printer.Width + 567
Printer.ScaleHeight = Printer.Height
GridTop = PrintGrid.Top: GridLeft = PrintGrid.Left - 0.5 * 567
LenLab.FontName = PrintGrid.Font.Name
LenLab.Font.Size = PrintGrid.Font.Size
DltH = (RowHei - LenLab.Height) \ 2
'-----------------------------------------------------------------
PageWidth = PrintGrid.Width
LineY = GridTop
For a = 0 To PrintGrid.Rows - 1
LineX = GridLeft
'画横线
Printer.Line (GridLeft, LineY)-(GridLeft + PageWidth, LineY)
For B = 0 To PrintGrid.Cols - 1
TmpWidth = PrintGrid.ColWidth(B)
If a = 0 Then
'输出表头,并画竖线
FixLab = PrintGrid.TextMatrix(0, B)
LenLab = FixLab: DoEvents
Printer.Line (LineX, GridTop)-(LineX, GridTop + PageHeight)
Printer.CurrentX = LineX + (TmpWidth - LenLab.Width) \ 2
Printer.CurrentY = LineY + DltH
Printer.Print FixLab
Else
'输出内容
Printer.CurrentX = LineX + 45
Printer.CurrentY = LineY + DltH
Printer.Print PrintGrid.TextMatrix(a, B)
End If
LineX = LineX + TmpWidth
If a = 0 And B = PrintGrid.Cols - 1 Then
Printer.Line (LineX, GridTop)-(LineX, GridTop + PageHeight)
End If
Next B
LineY = LineY + RowHei
'画最后一条线
If a = PrintGrid.Rows - 1 Then
Printer.Line (GridLeft, LineY)-(GridLeft + PageWidth, LineY)
End If
Next a
Label1 = "第" & PageStar & "页"
If GridLeft + PrintGrid.Width > PrintPic.Width - 1200 Then
Label1.Left = PrintPic.Width - 1200
Else
Label1.Left = GridLeft + PrintGrid.Width - Label1.Width
End If
Printer.CurrentX = Label1.Left
Printer.CurrentY = LineY + 210
Printer.Print Label1
DoEvents
If PageStar < PE Then
Printer.NewPage
Else
Printer.EndDoc
End If
Next PageStar
End Sub