当前位置: 源码素材网 » VB教程 » 详情页

一个VB版的Listbox列表框数据自动分页打印模块

  •   时间:2019-02-25
  • 概述:ListBox 列表分页 分页打印

VB6.0/vb.NET环境适用的列表框数据自动分页打印模块,代码中的注释还是挺多的,欢迎VB爱好者学习、改进使用:

Function listdataprint(listobj As ListBox, titleline As Integer, lineapage As Integer, lastline As Boolean, printfontsize As Integer, leftchar As Integer, topline As Integer, noline As Integer) As Boolean
'参数说明:
'listobj    列表框对象,即要打印的数据所在的列表框
'titleline  打印时的标题行数,即每页始终打印listobj中前titleline行
'lineapage  每页打印的行数,程序将按此参数自动分页打印
'lastline   该参数值为True时,每页最后将打印listobj中最后一行,若listobj中是表格,该参数就非常有用
'           该参数值为False时,则每页最后不打印listobj中最后一行
'topline    上边距行
Dim intpage As Integer            '整数页变量
Dim totalpage As Integer          '总页数变量
Dim lastpageline As Integer       '最后一页行数
Dim pagewidth As Integer          '所需纸张宽度
Dim pageheight As Integer          '所需纸张高度
Dim iloop As Integer              '循环变量
Dim jloop As Integer              '循环变量
Dim retval As Integer             '返回变量
Dim pwpara As Single
Dim phpara As Single
Dim linewidth As Integer
Dim pt As String

'检查listobj中是否有打印数据,无数据则返回
If listobj.ListCount = 0 Then
  listdataprint = False
  MsgBox Chr(13) + "没有数据可以打印!   ", vbCritical
  Exit Function
End If
'检测标题行数,小于1则返回
If titleline < 1 Then
  listdataprint = False
  MsgBox Chr(13) + "标题行数至少应为1!   ", vbCritical
  Exit Function
End If
'隐藏打印对象listobj,便于加快数据提取速度
listobj.Visible = False
'计算相关参数
intpage = (listobj.ListCount - titleline) \ lineapage
lastpageline = (listobj.ListCount - titleline) Mod lineapage
If lastpageline > 0 Then
  totalpage = intpage + 1
Else
  totalpage = intpage
End If
'计算所需纸张宽度
linewidth = 0
For iloop = 0 To listobj.ListCount - 1
  listobj.ListIndex = iloop
  'If linewidth < LenB(StrConv(listobj.Text, vbFromUnicode)) Then
  '  linewidth = LenB(StrConv(listobj.Text, vbFromUnicode))
    'LenB、StrConv两个函数用来返回中英文字符串的宽度(单位:Byte,即英文字符长度)
  If linewidth < strlens(listobj.Text) Then
    linewidth = strlens(listobj.Text)
  End If
Next iloop
listobj.ListIndex = 0
'每个英文字符的打印宽度
If printfontsize = 12 Then
  pwpara = 2.12
Else
  pwpara = 1.41
End If
pagewidth = Int(linewidth * pwpara) + 1
'计算所需纸张高度
'每行的打印高度
If printfontsize = 12 Then
  phpara = 4.225
Else
  phpara = 2.825
End If
pageheight = Int((titleline + lineapage + topline) * phpara) + 3
'确认打印
retval = MsgBox(Chr(13) + "标准纸张:" + Chr(13) + Chr(13) + "B5(16开):  195×270(mm)   " + Chr(13) + "A4:        210×296(mm)   " + Chr(13) + "宽行(8开): 388×270(mm)   " + Chr(13) + Chr(13) + Chr(13) + "本次打印共 " + CStr(totalpage) + " 页。   " + Chr(13) + Chr(13) + "所需纸张大小:宽度:" + CStr(pagewidth) + "mm,高度:" + CStr(pageheight) + "mm。   " + Chr(13) + Chr(13) + "是否开始打印作业?    ", vbQuestion + vbYesNo)
If retval = 6 Then
  '打印整数页部分
  If intpage > 0 Then
   '从第1页到第intpage页
   For iloop = 1 To intpage
     If MsgBox(Chr(13) + "是否打印第 " + CStr(iloop) + "/" + CStr(totalpage) + " 页?    ", vbQuestion + vbYesNo) = vbYes Then
      '当前标题,第一行用黑体,字体大小大3,其余行用宋体
      Printer.FontName = "宋体"
      Printer.FontSize = printfontsize
      For jloop = 1 To topline
        Printer.Print ""
      Next jloop
      For jloop = 0 To titleline - 1
        listobj.ListIndex = jloop
        If jloop = 0 Then
          Printer.FontName = "黑体"
          Printer.FontSize = printfontsize + 3
          If strlens(listobj.Text) * (printfontsize + 3) / printfontsize > linewidth Then
             If leftchar > 0 Then
               Printer.Print Space(leftchar) + listobj.Text
             Else
               Printer.Print listobj.Text
             End If
          Else
             If leftchar > 0 Then
               Printer.Print Space(leftchar) + Space(Int((linewidth - strlens(listobj.Text) * (printfontsize + 3) / printfontsize) * 0.38)) + listobj.Text
             Else
               Printer.Print Space(Int((linewidth - strlens(listobj.Text) * (printfontsize + 3) / printfontsize) * 0.38)) + listobj.Text
             End If
          End If
        Else
          Printer.FontName = "宋体"
          Printer.FontSize = printfontsize
          If jloop = noline Then
            If totalpage > 1 Then
              If leftchar > 0 Then
                pt = "页号:" + CStr(iloop) + "/" + CStr(totalpage)
                Printer.Print Space(leftchar) + listobj.Text + Space(linewidth - strlens(listobj.Text) - strlens(pt) - 1) + pt
              Else
                pt = "页号:" + CStr(iloop) + "/" + CStr(totalpage)
                Printer.Print listobj.Text + Space(linewidth - strlens(listobj.Text) - strlens(pt) - 1) + pt
              End If
            Else
              If leftchar > 0 Then
                Printer.Print Space(leftchar) + listobj.Text
              Else
                Printer.Print listobj.Text
              End If
            End If
          Else
            If leftchar > 0 Then
              Printer.Print Space(leftchar) + listobj.Text
            Else
              Printer.Print listobj.Text
            End If
          End If
        End If
      Next jloop
      '当前页除标题外的其它内容
      Printer.FontName = "宋体"
      Printer.FontSize = printfontsize
      '判断当前页是否打印listobj中最后一行
      If lastline = True Then
        '报表数据
        For jloop = (iloop - 1) * lineapage + titleline To iloop * lineapage + titleline - 2
          listobj.ListIndex = jloop
          If leftchar > 0 Then
            Printer.Print Space(leftchar) + listobj.Text
          Else
            Printer.Print listobj.Text
          End If
        Next jloop
        listobj.ListIndex = listobj.ListCount - 1
        If leftchar > 0 Then
          Printer.Print Space(leftchar) + listobj.Text
        Else
          Printer.Print listobj.Text
        End If
      Else
        '非报表数据
        For jloop = (iloop - 1) * lineapage + titleline To iloop * lineapage + titleline - 1
          listobj.ListIndex = jloop
          If leftchar > 0 Then
            Printer.Print Space(leftchar) + listobj.Text
          Else
            Printer.Print listobj.Text
          End If
        Next jloop
      End If
      '开始打印
      Printer.EndDoc
     End If
   Next iloop
  End If
  '最后一页
  If lastpageline > 0 Then
    If MsgBox(Chr(13) + "是否打印第 " + CStr(totalpage) + "/" + CStr(totalpage) + " 页?    ", vbQuestion + vbYesNo) = vbYes Then
    '当前页标题,第一行用黑体,字体大小大3,其余行用宋体
    Printer.FontName = "宋体"
    Printer.FontSize = printfontsize
    For jloop = 1 To topline
      Printer.Print ""
    Next jloop
    For jloop = 0 To titleline - 1
      listobj.ListIndex = jloop
      If jloop = 0 Then
        Printer.FontName = "黑体"
        Printer.FontSize = printfontsize + 3
        If strlens(listobj.Text) * (printfontsize + 3) / printfontsize > linewidth Then
          If leftchar > 0 Then
            Printer.Print Space(leftchar) + listobj.Text
          Else
            Printer.Print listobj.Text
          End If
        Else
          If leftchar > 0 Then
            Printer.Print Space(leftchar) + Space(Int((linewidth - strlens(listobj.Text) * (printfontsize + 3) / printfontsize) * 0.38)) + listobj.Text
          Else
            Printer.Print Space(Int((linewidth - strlens(listobj.Text) * (printfontsize + 3) / printfontsize) * 0.38)) + listobj.Text
          End If
        End If
      Else
        Printer.FontName = "宋体"
        Printer.FontSize = printfontsize
        If jloop = noline Then
          If totalpage > 1 Then
            If leftchar > 0 Then
              pt = "页号:" + CStr(totalpage) + "/" + CStr(totalpage)
              Printer.Print Space(leftchar) + listobj.Text + Space(linewidth - strlens(listobj.Text) - strlens(pt) - 1) + pt
            Else
              pt = "页号:" + CStr(totalpage) + "/" + CStr(totalpage)
              Printer.Print listobj.Text + Space(linewidth - strlens(listobj.Text) - strlens(pt) - 1) + pt
            End If
          Else
            If leftchar > 0 Then
              Printer.Print Space(leftchar) + listobj.Text
            Else
              Printer.Print listobj.Text
            End If
          End If
        Else
          If leftchar > 0 Then
            Printer.Print Space(leftchar) + listobj.Text
          Else
            Printer.Print listobj.Text
          End If
        End If
      End If
    Next jloop
    '当前页除标题外的其它内容及最后一行
    Printer.FontName = "宋体"
    Printer.FontSize = printfontsize
    For jloop = (totalpage - 1) * lineapage + titleline To listobj.ListCount - 1
      listobj.ListIndex = jloop
      If leftchar > 0 Then
        Printer.Print Space(leftchar) + listobj.Text
      Else
        Printer.Print listobj.Text
      End If
    Next jloop
    '开始打印
    Printer.EndDoc
    End If
  End If
End If
'显示打印对象listobj
listobj.Visible = True
End Function

    相关声明:

      若“一个VB版的Listbox列表框数据自动分页打印模块”有损您的权益,请告之我们删除内容。
      部分文章来源于网络,版权归原作者所有。