---- 几乎所有数据库应用软件,都需要打印单证和报表。常见的方法是利用VB的Crystal Reports(水晶报表)方式,通过TextBox等数据绑定控件,调用Print方法直接输出。虽然Crystal Report这一个功能强大、样式丰富且无编程方式的报表编制程序能适应大部分单证、报表打印的需要,但是Crystal Reports引擎是一个动态链接库,需许多文件支持和调用更多系统资源,大大增加系统负担。
---- 另一种解决办法是通过Printer对象的Print方法,直接打印字符串。这虽然减少了系统资源的开销,但它不能直接用于打印复杂的单证和报表。本文通过两个实例,阐述一个通用折行打印程序的编程和在单证及报表的应用。
---- 二. 编程实现及实例
---- 为便于阐述的方便,我们先建立一个Access数据库Standards.mdb,其内SN表由以下几个字段组成:
---- 标准号(文本,17)
---- 标准名称(文本,255)
---- 英文名称(文本,255)
---- 实施日期(日期,8)
---- 修定日期(日期,8)
---- 发布日期(日期,8)
---- 代替标准(文本,50)
---- 通用折行打印程序编制操作如下:
---- 1.在VB5.0编程环境中,新建一个工程Project1;
---- 2. 在Project1中添加Moduel,在Moduel模块中定义一个记录最大折行数的公用变量Rowlab和Function函数(以下程序都经过实际运行测试,可以原样复制使用);
Public rowlab As Integer
'定义一个公用变量
Function prnt11(X As Integer, Y As
Integer,Font As Single, Txt As String, Val As Integer)
Dim str As String, str1 As String,
str2 As String ,i As Integer
Printer.CurrentX = X
Printer.CurrentY = Y
Printer.FontBold = False
Printer.FontSize = font
str = txt
str2 = str
i = 0
rowlab = 0
If Len(Trim(str)) = 0 Then
rowlab = 1 '待打印字符串为空的标志
Else
Do While Len(str) > 0
Printer.CurrentX = X
Printer.CurrentY = Y + rowlab * 240
rowlab = rowlab + 1
If Len(str) >= val Then
str1 = Mid(str, 1, val)
Printer.Print str1
i = i + 1
str = Mid(str2, i * val + 1)
Else
Printer.Print str
Exit Do
End If
Loop
End If
End Function
---- 3. 在Project1中新建一个窗体Form1,窗体上添加一个Data控件Data1,一个MSFlexGrid控件MSFGrid1,7个TextBox和两个命令按钮CmdPrnt1、CmdPrnt2。设置Data控件的属性:
.. DatabaseName="Standards.mdb"
..RecordSourse="SN"
MSFGrid1属性:
.DataSource="Data1"
Text1属性:
.DataSource="Data1"
.DataField="标准号"
Text2~Text7类同。
---- CmdPrnt1、CmdPrnt2分别为打印单条记录和多条记录的按钮。
---- 实例1:文字串定位折行打印在口岸联检部门中应用非常广泛。下述例子是用CmdPrnt1的Click事件代码实现了对文字串定位折行打印:
Private Sub CmdPrnt1_Click()
Dim str As String, str1 As String ,txt As String
Dim strx As Integer, stry As Integer,i As Integer
strx = 200
stry = 0
txt = Space(20) + "中国出入境检验检疫标准目录检索STEMS 2000"
Printer.FontName = "黑体"
dd = prnt11(strx, stry, 10, txt, 50)
stry = stry + rowlab * 240
Printer.Line (0, stry)-(9000, stry)
Printer.FontName = "宋体"
txt = "标准号:" + Space(2) + Trim(Text1) + Space(3) +
"发布日期:" + Trim(Text4) + Space(3) + "实施日期:"
+ Trim(Text6) + Space(3) + "修定日期:" + Trim(Text5) '+ Chr(13)
stry = stry + 240
dd = prnt11(strx, stry, 10, txt, 70)
stry = stry + rowlab * 240
txt = "代替标准:"
dd = prnt11(strx, stry, 10, txt, 10)
dd = prnt11(strx + 1000, stry, 10, Trim(Text7), 60)
stry = stry + rowlab * 240
txt = "标准名称:"
dd = prnt11(strx, stry, 10, txt, 10)
dd = prnt11(strx + 1000, stry, 10, Trim(Text4), 38)
stry = stry + rowlab * 240
txt = "英文名称:"
dd = prnt11(strx, stry, 10, txt, 10)
dd = prnt11(strx + 1000, stry, 10, Text5, 72)
Printer.EndDoc
End Sub
---- 注: Prnt11函数原形:prnt11(X As Integer, Y As Integer, Font As Single, Txt As String, Val As Integer),其各参数含义如下:
---- X、Y为待打印字符串左上角起始座标;
---- Font为字体大小;
---- Txt为待打印字符串;
---- Val为字符串打印折行长度。
---- 实例2:直接打印表格式窗体显示的多记录多字段,往往因某些字段的字节太多而造成纸张宽度不足。以下CmdPrnt2的Click事件中的代码,实现了对上述MSFGrid1表格记录的打印:
Private Sub CmdPrnt2_Click()
Dim fnt As Single
Dim pp As Integer
Dim stry As Integer, strx As Integer
Dim stry1 As Integer,
strx1 As Integer, linw As Integer
Dim page1 As Integer, p As Integer,
gridrow As Integer, ii As Integer
p = 0
ii = 1 'ii记录MSFGRID1表格同一记录内字段最大打印行
pp = 0 '开始页码
ss$ = "中国出入境检验检疫标准目录检索STEMS 2000" ' 表头
Static a(4) As Integer
kan = 0
a(2) = 1680
a(3) = 2800
a(4) = 5300
page1 = 46 '定义每页行数
strx = 200
strx1 = 200
stry = 1400
stry1 = 1400
linw = 240 '定义行宽
fnt = 10 '定义字体大小
For i = 2 To 4
kan = kan + a(i)
Next
gridrow = Datdatact1.Recordset.RecordCount
If gridrow = 0 Then
MsgBox "无满足条件记录打印!"
Exit Sub
End If
Printer.FontName = "黑体"
dd = prnt11(3300, 700, fnt, ss$, 26) '打印标题
dd = prnt11(500, stry - 250, fnt, "标准号", 26)
dd = prnt11(2690, stry - 250, fnt, "标准名称", 26)
dd = prnt11(6690, stry - 250, fnt, "英文名称", 26)
Printer.Line (strx - 20, stry - 30)-(10460, stry - 30)
Printer.FontName = "宋体"
For j = 1 To gridrow '打印gridrow条记录
MSFGrid1.Row = j
strx = strx1
For i = 2 To 4 '假设只打印2-3 列
MSFGrid1.Col = i
dd = prnt11(strx, stry, fnt,
MSFGrid1.Text, IIf(i = 3, 13, 55))
If ii < rowlab Then
'ii记录同一记录内字段最大打印行
ii = rowlab
End If
strx = strx + a(i)
Next
p = p + ii
rowlab = ii
ii = 1 '重新初始化
If p > page1 Then
p = 0
strx = strx1
For n = 2 To 4
strx = strx + a(n)
Next
pp = pp + 1
stry = stry + rowlab * linw
foot$ = "第" + CStr(pp) + "页"
dd = prnt11(strx / 2, stry + 3 * linw, 10, foot$, 26)
stry = stry1
Printer.NewPage
Printer.FontName = "黑体"
dd = prnt11(3300, 700, fnt, ss$, 26)
dd = prnt11(500, stry - 250, fnt, "标准号", 26)
dd = prnt11(2690, stry - 250, fnt, "标准名称", 26)
dd = prnt11(6690, stry - 250, fnt, "英文名称", 26)
Printer.Line (-20, stry - 30)-(10460, stry - 30)
'打印起始线
Printer.FontName = "宋体"
strx = strx1
Else
stry = stry + rowlab * linw
End If
Next
If p < page1 Then
For p = 0 To page1 + 1
strx = strx1
Next
End If
strx = strx1
For n = 2 To 4
strx = strx + a(n)
Next
pp = pp + 1
foot$ = "第" + CStr(pp) + "页"
dd = prnt11(strx / 2, stry + 3 * linw, 10, foot$, 26)
Printer.EndDoc
End Sub