⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printtable.vb

📁 vb.net写的图书管理系统,功能完备,能正常使用,可用于二次开发.
💻 VB
字号:
Public Class PrintTable
    Public Sub New()

    End Sub
    Public Sub New(ByVal myTable As DataTable)
        Me.myTable = myTable
    End Sub
    '设置标题信息
    Public myTitle As String
    Public mySubTitle1 As String
    Public mySubTitle2 As String
    Public myBottomTitle1 As String
    Public myBottomTitle2 As String
    Public myBottomTitle3 As String
    '数据来源
    Private myTable As DataTable
    Private WithEvents printdoc As New Printing.PrintDocument
    '错误消息
    Private Const ErrorMSG As String = "Error occor! Please check where has erroe!"
    '页宽
    Public myPageWidth As Double = 786
    '页高
    Public myPageHeight As Double = 1050
    '上边距
    Public myPageTopMargin As Double = 50
    '左边距
    Public myPageLeftMargin As Double = 20
    '字体
    Public DataFont As Font = New Font("Arial", 10, FontStyle.Regular)
    '表格式
    Public TableFont As Font = New Font("Arial", 14, FontStyle.Bold)
    '数据颜色
    Public DataPen As Pen = New Pen(Color.Red)
    '表格颜色
    Public TablePen As Pen = New Pen(Color.SkyBlue)
    Public DataBrush As New SolidBrush(Color.Red)
    Public TableBrush As New SolidBrush(Color.Black)
    '列名称
    Private arrColName As ArrayList
    '列宽度
    Private arrColLength As ArrayList
    '列
    Private arrCol As ArrayList
    '列格式
    Private arrColFormat As ArrayList
    '位置
    Private arrLocationX As ArrayList
    '行数
    Private myPageRowsCount As Integer
    '行高
    Private myPageRowsHight As Integer

    Private Function PreProcess() As Boolean
        '判断数据是否为空
        If myTable Is Nothing Then
            MsgBox(ErrorMSG)
            Return False
        End If
        '首先计算列宽
        Dim i As Integer
        Dim n As Integer
        Dim colLength As Double
        Dim allLength As Double
        arrColLength = New ArrayList
        For i = 0 To myTable.Columns.Count - 1
            allLength += myTable.Columns(i).Caption.Length
        Next
        For i = 0 To myTable.Columns.Count - 1
            colLength = myTable.Columns(i).Caption.Length * myPageWidth / allLength
            arrColLength.Add(colLength)
        Next
        '然后计算行高
        myPageRowsHight = DataFont.Height
        myPageRowsCount = myPageHeight / myPageRowsHight
        '设置表格格式
        Dim myTableFormat As StringFormat
        arrColFormat = New ArrayList
        For i = 0 To myTable.Columns.Count - 1
            myTableFormat = New StringFormat
            myTableFormat.Alignment = StringAlignment.Center
            myTableFormat.LineAlignment = StringAlignment.Center
            myTableFormat.Trimming = StringTrimming.Word
            arrColFormat.Add(myTableFormat)
        Next
        '设置位置
        Dim locationX As Double
        arrLocationX = New ArrayList
        locationX = myPageLeftMargin
        arrLocationX.Add(locationX)
        For i = 1 To myTable.Columns.Count - 1
            locationX = arrLocationX(i - 1) + arrColLength(i - 1)
            arrLocationX.Add(locationX)
        Next
        Return True
    End Function

    '打印预览
    Public Function printViewer()
        Try
            If PreProcess() Then
                Dim mprintViewer As New PrintPreviewDialog
                mprintViewer.Document = printdoc
                mprintViewer.ShowDialog()
            End If
        Catch ex As Exception
            MsgBox(ErrorMSG + ex.ToString)
        End Try
    End Function

    '打印
    Public Function printer()
        Try
            If PreProcess() Then
                printdoc.Print()
            End If
        Catch ex As Exception
            MsgBox(ErrorMSG)
        End Try
    End Function

    'print sub
    Private Sub printdoc_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printdoc.PrintPage
        Try
            Dim currentPageIndex As Integer = 0
            Static currentIndex As Integer = 0
            Static currentPage As Integer = 0
            Dim i As Integer
            Dim n As Integer
            Dim locationY As Double
            Dim bounds As RectangleF
            Dim g As Drawing.Graphics = e.Graphics
            e.HasMorePages = False
            currentPage = currentPage + 1
            '开始打印
            '打印列标题
            For i = 0 To myTable.Columns.Count - 1
                locationY = myPageTopMargin
                bounds = New RectangleF(arrLocationX(i), locationY, arrColLength(i), myPageRowsHight)
                g.DrawRectangle(TablePen, bounds.X, bounds.Y, bounds.Width, bounds.Height)
                g.DrawString(myTable.Columns(i).Caption, DataFont, DataBrush, bounds, arrColFormat(i))
            Next
            '打印当前页
            For i = currentIndex To myTable.Rows.Count - 1
                currentPageIndex = currentPageIndex + 1
                locationY = myPageTopMargin + myPageRowsHight * currentPageIndex
                Debug.WriteLine("locationy=" & locationY)
                For n = 0 To myTable.Columns.Count - 1
                    bounds = New RectangleF(arrLocationX(n), locationY, arrColLength(n), myPageRowsHight)
                    g.DrawRectangle(TablePen, bounds.X, bounds.Y, bounds.Width, bounds.Height)
                    g.DrawString(myTable.Rows(i).Item(n) & "", DataFont, DataBrush, bounds, arrColFormat(n))
                Next
                currentIndex = i
                If currentIndex >= myTable.Rows.Count Then
                    Exit Sub
                Else
                    If currentPageIndex >= myPageRowsCount Then
                        currentPageIndex = i
                        e.HasMorePages = True
                        Exit Sub
                    End If
                End If
            Next
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -