基于VB-PRINTER对象的动态报表打印[图](3)
' 注:因为打印是以缇为单位的,1厘米=567缇,A4纸大小29.7厘米21厘米,所以转换为缇为单位的大小是16839.9缇×11907缇,打印中横、纵向不能超过这二个数,上面程序设定横不超边14000缇,纵不超过10500缇,用变量L表示
3.2 打印单页过程
Private Sub Print_Grid2(ByVal StartRow As Integer, ByVal EndRow As Integer, _
ByVal StartCol As Integer, ByVal EndCol As Integer)
Dim Px, Py, x1, y1, i, j As Integer
Dim L As Long, W As Long
x1 = 1000 : y1 = 1000 '设定表格线的左上基准点坐标位置,单位为缇
Px = 100 : Py = 50 '设置打印的数据离左表格线、上表格线的距离
With MSHFlexGrid1
'打印第一行
Printer.CurrentX = x1 + Px
Printer.CurrentY = y1 + Py
Printer.Print .TextMatrix(0, 0)
W = .ColWidth(0)
For j = StartCol To EndCol
Printer.CurrentX = x1 + W + Px
Printer.CurrentY = y1 + Py
Printer.Print .TextMatrix(0, j)
W = W + .ColWidth(j)
Next j
L = .RowHeight(0)
Printer.Line (x1, y1)-(x1 + W, y1)
Printer.Line (x1, y1 + L)-(x1 + W, y1 + L)
'各行数据,表格线
For i = StartRow To EndRow
Printer.CurrentX = x1 + Px '第一列
Printer.CurrentY = y1 + L + Py
Printer.Print .TextMatrix(i, 0)
W = .ColWidth(0)
For j = StartCol To EndCol
Printer.CurrentX = x1 + W + Px
Printer.CurrentY = y1 + L + Py
Printer.Print .TextMatrix(i, j)
W = W + .ColWidth(j)
Next j
L = L + .RowHeight(i)
Printer.Line (x1, L + y1)-(x1 + W, L + y1)
Next i
'竖线打印
Printer.Line (x1, y1)-(x1, L + y1)
W = 0
W = W + .ColWidth(0)
Printer.Line (x1 + W, y1)-(x1 + W, L + y1)
For j = StartCol To EndCol
W = W + .ColWidth(j)
Printer.Line (x1 + W, y1)-(x1 + W, L + y1)
Next j
End With
End Sub
4 结束语
利用MSHFlexGrid控件与Printer对象的配合,采用两个过程,实现动态报表的显示与打印,解决了其他方法在VB上难以实现的动态报表输出问题,具有很强的实用性。