实现水晶报表的打印功能(网摘)

 
  1. Imports System.Drawing
  2. Imports System.Drawing.Pen
  3. Imports System.Drawing.Font
  4. Imports System.Drawing.PointF
  5. Imports System.Drawing.Color
  6. Imports System.Drawing.Printing
  7. Imports System.Windows.Forms
  8. Imports System.Windows.Forms.DataGrid
  9. Public Class PrintDataTable
  10.     '以下用户可自定义
  11.     '当前要打印文本的字体及字号
  12.     Private TableFont As New Font("宋体", 10)
  13.     '表头字体
  14.     Private HeadFont As New Font("宋体", 20, FontStyle.Bold)
  15.     '副表头字体
  16.     Private SubHeadFont As New Font("宋体", 10, FontStyle.Regular)
  17.     '表头文字
  18.     Private HeadText As String
  19.     '副表头左文字
  20.     Private SubHeadLeftText As String
  21.     '副表头右文字
  22.     Private SubHeadRightText As String
  23.     '表头高度
  24.     Private HeadHeight As Integer = 40
  25.     '副表头高度
  26.     Private SubHeadHeight As Integer = 20
  27.     '表脚字体
  28.     Private FootFont As New Font("宋体", 10, FontStyle.Regular)
  29.     '副表脚字体
  30.     Private SubFootFont As New Font("宋体", 10, FontStyle.Regular)
  31.     '表脚文字
  32.     Private FootText As String
  33.     '副表脚左文字
  34.     Private SubFootLeftText As String
  35.     '副表脚右文字
  36.     Private SubFootRightText As String
  37.     '表脚高度
  38.     Private FootHeight As Integer = 30
  39.     '副表脚高度
  40.     Private SubFootHeight As Integer = 20
  41.     '表的基本单位
  42.     Dim XUnit As Integer
  43.     Dim YUnit As Integer = TableFont.Height * 2.5
  44.     '以下为模块内部使用
  45.     Private Ev As PrintPageEventArgs
  46.     Private DataTablePrinter As PrintDocument
  47.     Private DataGridColumn As DataColumn
  48.     Private DataGridRow As DataRow
  49.     Private DataTablePrint As DataTable
  50.     '当前要打印的行
  51.     Private Rows As Integer
  52.     '当前DATAGRID共有多少列
  53.     Private ColsCount As Integer
  54.     '当前正要打印的行号
  55.     Private PrintingLineNumber As Integer
  56.     '当前要所要打印的记录行数,由计算得到
  57.     Private PageRecordNumber As Integer
  58.     '正要打印的页号
  59.     Private PrintingPageNumber As Integer
  60.     '共需要打印的页数
  61.     Private PageNumber As Integer
  62.     '当前还有多少页没有打印
  63.     Private PrintRecordLeave As Integer
  64.     '已经打印完的记录数
  65.     Private PrintRecordComplete As Integer
  66.     Private PLeft As Integer
  67.     Private PTop As Integer
  68.     Private PRight As Integer
  69.     Private PBottom As Integer
  70.     Private PWidth As Integer
  71.     Private PHeigh As Integer
  72.     '当前画笔颜色
  73.     Private DrawBrush As New SolidBrush(System.Drawing.Color.Black)
  74.     '每页打印的记录条数
  75.     Private PrintRecordNumber As Integer
  76.     '总共应该打印的页数
  77.     Private TotalPage As Integer
  78.     Sub New(ByVal TableSource As DataTable)
  79.         DataTablePrint = New DataTable
  80.         DataTablePrint = TableSource
  81.         ColsCount = DataTablePrint.Columns.Count
  82.     End Sub
  83.     '用户自定义字体及字号
  84.     Public WriteOnly Property SetTableFont() As System.Drawing.Font
  85.         Set(ByVal Value As System.Drawing.Font)
  86.             TableFont = Value
  87.         End Set
  88.     End Property
  89.     Public WriteOnly Property SetHeadFont() As System.Drawing.Font
  90.         Set(ByVal Value As System.Drawing.Font)
  91.             HeadFont = Value
  92.         End Set
  93.     End Property
  94.     Public WriteOnly Property SetSubHeadFont() As System.Drawing.Font
  95.         Set(ByVal Value As System.Drawing.Font)
  96.             SubHeadFont = Value
  97.         End Set
  98.     End Property
  99.     Public WriteOnly Property SetFootFont() As System.Drawing.Font
  100.         Set(ByVal Value As System.Drawing.Font)
  101.             FootFont = Value
  102.         End Set
  103.     End Property
  104.     Public WriteOnly Property SetSubFootFont() As System.Drawing.Font
  105.         Set(ByVal Value As System.Drawing.Font)
  106.             SubFootFont = Value
  107.         End Set
  108.     End Property
  109.     Public WriteOnly Property SetHeadText() As String
  110.         Set(ByVal Value As String)
  111.             HeadText = Value
  112.         End Set
  113.     End Property
  114.     Public WriteOnly Property SetSubHeadLeftText() As String
  115.         Set(ByVal Value As String)
  116.             SubHeadLeftText = Value
  117.         End Set
  118.     End Property
  119.     Public WriteOnly Property SetSubHeadRightText() As String
  120.         Set(ByVal Value As String)
  121.             SubHeadRightText = Value
  122.         End Set
  123.     End Property
  124.     Public WriteOnly Property SetFootText() As String
  125.         Set(ByVal Value As String)
  126.             FootText = Value
  127.         End Set
  128.     End Property
  129.     Public WriteOnly Property SetSubFootLeftText() As String
  130.         Set(ByVal Value As String)
  131.             SubFootLeftText = Value
  132.         End Set
  133.     End Property
  134.     Public WriteOnly Property SetSubFootRightText() As String
  135.         Set(ByVal Value As String)
  136.             SubFootRightText = Value
  137.         End Set
  138.     End Property
  139.     Public WriteOnly Property SetHeadHeight() As Integer
  140.         Set(ByVal Value As Integer)
  141.             HeadHeight = Value
  142.         End Set
  143.     End Property
  144.     Public WriteOnly Property SetSubHeadHeight() As Integer
  145.         Set(ByVal Value As Integer)
  146.             SubHeadHeight = Value
  147.         End Set
  148.     End Property
  149.     Public WriteOnly Property SetFootHeight() As Integer
  150.         Set(ByVal Value As Integer)
  151.             FootHeight = Value
  152.         End Set
  153.     End Property
  154.     Public WriteOnly Property SetSubFootHeight() As Integer
  155.         Set(ByVal Value As Integer)
  156.             SubFootHeight = Value
  157.         End Set
  158.     End Property
  159.     Public WriteOnly Property SetCellHeight() As Integer
  160.         Set(ByVal Value As Integer)
  161.             YUnit = Value
  162.         End Set
  163.     End Property
  164.     Public Sub Print()
  165.         Try
  166.             DataTablePrinter = New Printing.PrintDocument
  167.             AddHandler DataTablePrinter.PrintPage, AddressOf DataTablePrinter_PrintPage
  168.             Dim PageSetup As PageSetupDialog
  169.             PageSetup = New PageSetupDialog
  170.             PageSetup.Document = DataTablePrinter
  171.             DataTablePrinter.DefaultPageSettings = PageSetup.PageSettings
  172.             If PageSetup.ShowDialog() = DialogResult.Cancel Then
  173.                 Exit Sub
  174.             End If
  175.             PLeft = DataTablePrinter.DefaultPageSettings.Margins.Left
  176.             PTop = DataTablePrinter.DefaultPageSettings.Margins.Top
  177.             PRight = DataTablePrinter.DefaultPageSettings.Margins.Right
  178.             PBottom = DataTablePrinter.DefaultPageSettings.Margins.Bottom
  179.             PWidth = DataTablePrinter.DefaultPageSettings.Bounds.Width
  180.             PHeigh = DataTablePrinter.DefaultPageSettings.Bounds.Height
  181.             '将当前页分成基本的单元
  182.             XUnit = (PWidth - PLeft - PRight) / DataTablePrint.Columns.Count - 1
  183.             PrintRecordNumber = (PHeigh - PTop - PBottom - HeadHeight - SubHeadHeight - FootHeight - SubFootHeight - YUnit) / YUnit
  184.             If DataTablePrint.Rows.Count > PrintRecordNumber Then
  185.                 If DataTablePrint.Rows.Count Mod PrintRecordNumber = 0 Then
  186.                     TotalPage = DataTablePrint.Rows.Count / PrintRecordNumber
  187.                 Else
  188.                     TotalPage = DataTablePrint.Rows.Count / PrintRecordNumber + 1
  189.                 End If
  190.             Else
  191.                 TotalPage = 1
  192.             End If
  193.             DataTablePrinter.DocumentName = TotalPage.ToString
  194.             Dim PrintPriview As PrintPreviewDialog
  195.             PrintPriview = New PrintPreviewDialog
  196.             PrintPriview.Document = DataTablePrinter
  197.             PrintPriview.WindowState = FormWindowState.Maximized
  198.             PrintPriview.ShowDialog()
  199.         Catch ex As Exception
  200.             MsgBox("打印错误,请检查打印设置!", 16, "错误")
  201.         End Try
  202.     End Sub
    1. Private Sub DataTablePrinter_PrintPage(ByVal sender As ObjectByVal Ev As System.Drawing.Printing.PrintPageEventArgs)
    2.         '还有多少条记录没有打印
    3.         PrintRecordLeave = DataTablePrint.Rows.Count - PrintRecordComplete
    4.         If PrintRecordLeave > 0 Then
    5.             If PrintRecordLeave Mod PrintRecordNumber = 0 Then
    6.                 PageNumber = PrintRecordLeave / PrintRecordNumber
    7.             Else
    8.                 PageNumber = PrintRecordLeave / PrintRecordNumber + 1
    9.             End If
    10.         Else
    11.             PageNumber = 0
    12.         End If
    13.         '正在打印的页数,因为每打印一个新页都要计算还有多少页没有打印所以以打印的页数初始为0
    14.         PrintingPageNumber = 0
    15.         '计算,余下的记录条数是否还可以在一页打印,不满一页时为假
    16.         If DataTablePrint.Rows.Count - PrintingPageNumber * PrintRecordNumber >= PrintRecordNumber Then
    17.             PageRecordNumber = PrintRecordNumber
    18.         Else
    19.             PageRecordNumber = (DataTablePrint.Rows.Count - PrintingPageNumber * PrintRecordNumber) Mod PrintRecordNumber
    20.         End If
    21.         Dim fmt As New StringFormat
    22.         '上下对齐
    23.         fmt.LineAlignment = StringAlignment.Center
    24.         '自动换行
    25.         fmt.FormatFlags = StringFormatFlags.LineLimit
    26.         '打印区域
    27.         Dim Rect As New Rectangle
    28.         '打印表格线格式
    29.         Dim Pen As New Pen(Brushes.Black, 1)
    30.         While PrintingPageNumber <= PageNumber
    31.             '表头中间对齐
    32.             fmt.Alignment = StringAlignment.Center
    33.             '表头和副表头宽度等于设置区域宽度
    34.             Rect.Width = PWidth - PLeft - PRight
    35.             Rect.Height = HeadHeight
    36.             Rect.X = PLeft
    37.             Rect.Y = PTop
    38.             '打印表头
    39.             Ev.Graphics.DrawString(HeadText, HeadFont, Brushes.Black, RectangleF.op_Implicit(Rect), fmt)
    40.             '副表头左对齐
    41.             fmt.Alignment = StringAlignment.Near
    42.             Rect.Width = (PWidth - PLeft - PRight) / 2 - 1
    43.             Rect.Height = SubHeadHeight
    44.             Rect.Y = PTop + HeadHeight
    45.             '打印副表头左
    46.             Ev.Graphics.DrawString(SubHeadLeftText, SubHeadFont, Brushes.Black, RectangleF.op_Implicit(Rect), fmt)
    47.             '右副表头文字从右往左排列
    48.             fmt.FormatFlags = StringFormatFlags.DirectionRightToLeft
    49.             '右副表头右对齐
    50.             fmt.Alignment = StringAlignment.Near
    51.             Rect.X = PLeft + (PWidth - PLeft - PRight) / 2
    52.             '打印副表头右
    53.             Ev.Graphics.DrawString(SubHeadRightText, SubHeadFont, Brushes.Black, RectangleF.op_Implicit(Rect), fmt)
    54.             fmt.Alignment = StringAlignment.Center
    55.             Rect.X = PLeft
    56.             Rect.Y = PTop + HeadHeight + SubHeadHeight + (PrintRecordNumber + 1) * (YUnit) + SubFootHeight
    57.             Rect.Height = FootHeight
    58.             Rect.Width = PWidth - PLeft - PRight
    59.             '打印表脚
    60.             Ev.Graphics.DrawString(FootText, FootFont, Brushes.Black, RectangleF.op_Implicit(Rect), fmt)
    61.             '副表左左对齐
    62.             fmt.Alignment = StringAlignment.Far
    63.             Rect.X = PLeft
    64.             Rect.Y = PTop + HeadHeight + SubHeadHeight + (PrintRecordNumber + 1) * (YUnit)
    65.             Rect.Height = SubFootHeight
    66.             Rect.Width = (PWidth - PLeft - PRight) / 2 - 1
    67.             '打印左表脚
    68.             Ev.Graphics.DrawString(SubFootLeftText, SubFootFont, Brushes.Black, RectangleF.op_Implicit(Rect), fmt)
    69.             '副表头右对齐
    70.             fmt.Alignment = StringAlignment.Near
    71.             Rect.X = PLeft + (PWidth - PLeft - PRight) / 2
    72.             If DataTablePrint.Rows.Count = 0 Then
    73.                 SubFootRightText = "第" & TotalPage & "页,共" & TotalPage & "页"
    74.             Else
    75.                 SubFootRightText = "第" & TotalPage - PageNumber + 1 & "页,共" & TotalPage & "页"
    76.             End If
    77.             '打印右表脚
    78.             Ev.Graphics.DrawString(SubFootRightText, SubFootFont, Brushes.Black, RectangleF.op_Implicit(Rect), fmt)
    79.             '得到datatable的所有列名
    80.             fmt.Alignment = StringAlignment.Center
    81.             Dim ColumnText(DataTablePrint.Columns.Count) As String
    82.             For Cols = 0 To DataTablePrint.Columns.Count - 1
    83.                 '得到当前所有的列名
    84.                 ColumnText(Cols) = DataTablePrint.Columns(Cols).ToString
    85.                 Rect.X = PLeft + XUnit * Cols
    86.                 Rect.Y = PTop + HeadHeight + SubHeadHeight
    87.                 Rect.Width = XUnit
    88.                 Rect.Height = YUnit
    89.                 Ev.Graphics.DrawString(ColumnText(Cols), New Font(TableFont, FontStyle.Bold), DrawBrush, RectangleF.op_Implicit(Rect), fmt)
    90.                 Ev.Graphics.DrawRectangle(Pen, Rect)
    91.             Next
    92.             '结束---------------------得到datatable的所有列名
    93.             '当前页面已经打印的记录行数
    94.             Dim PrintingLine As Integer = 0
    95.             While PrintingLine < PageRecordNumber
    96.                 '确定要当前要打印的记录的行号
    97.                 DataGridRow = DataTablePrint.Rows(PrintRecordComplete)
    98.                 For Cols = 0 To DataTablePrint.Columns.Count - 1
    99.                     Rect.X = PLeft + XUnit * Cols
    100.                     Rect.Y = PTop + HeadHeight + SubHeadHeight + (PrintingLine + 1) * (YUnit)
    101.                     Rect.Width = XUnit
    102.                     Rect.Height = YUnit
    103.                     If DataGridRow(ColumnText(Cols)) Is System.DBNull.Value = False Then
    104.                         Ev.Graphics.DrawString(DataGridRow(ColumnText(Cols)), TableFont, DrawBrush, RectangleF.op_Implicit(Rect), fmt)
    105.                     End If
    106.                     Ev.Graphics.DrawRectangle(Pen, Rect)
    107.                 Next
    108.                 PrintingLine += 1
    109.                 PrintRecordComplete += 1
    110.                 If PrintRecordComplete >= DataTablePrint.Rows.Count Then
    111.                     Ev.HasMorePages = False
    112.                     PrintRecordComplete = 0
    113.                     Exit Sub
    114.                 End If
    115.             End While
    116.             PrintingPageNumber += 1
    117.             If PrintingPageNumber >= PageNumber Then
    118.                 Ev.HasMorePages = False
    119.             Else
    120.                 Ev.HasMorePages = True
    121.                 Exit While
    122.             End If
    123.         End While
    124.     End Sub
    125.     '附转换函数
    126.     'listview转为Datatable函数
    127.     Public Function ListviewToDatatable(ByVal Listview1 As ListView)
    128.         Dim Table1 As New DataTable
    129.         Dim i As Integer
    130.         Dim datacol As DataColumn
    131.         For i = 0 To Listview1.Columns.Count - 1
    132.             datacol = New DataColumn
    133.             datacol.DataType = Type.GetType("System.Object")
    134.             datacol.ColumnName = Listview1.Columns(i).Text.Trim
    135.             Table1.Columns.Add(datacol)
    136.         Next i
    137.         Dim j As Integer
    138.         Dim Datarow1 As DataRow
    139.         For j = 0 To Listview1.Items.Count - 1
    140.             Datarow1 = Table1.NewRow
    141.             For i = 0 To Listview1.Columns.Count - 1
    142.                 Datarow1.Item(Listview1.Columns(i).Text.Trim) = Listview1.Items(j).SubItems(i).Text.ToString
    143.             Next i
    144.             Table1.Rows.Add(Datarow1)
    145.         Next j
    146.         Return Table1
    147.     End Function
    148.     '数据集转为Datatable函数
    149.     Public Function DataBaseToDataTable(ByVal SqlDataAdapter1 As Data.SqlClient.SqlDataAdapter, ByVal TableName As String)
    150.         Dim Table1 As New DataTable
    151.         Dim DataSet1 As New DataSet
    152.         DataSet1.Clear()
    153.         SqlDataAdapter1.Fill(DataSet1, TableName)
    154.         Table1 = DataSet1.Tables(TableName)
    155.         Return Table1
    156.     End Function
    157. End Class
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值