在网上找到一个.net的打印类,打印Dataset的,不错,保存于止

简介: ''======================================= 'DATAGRID控件通用打印类 '中和科技-孙利臣 '于2003年05月27日17:05 '用于打印DATAGRID控件中的数据.
img_a6339ee3e57d1d52bc7d02b338e15a60.gif ' '======================================= 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif'
DATAGRID控件通用打印类 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif'
中和科技-孙利臣 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif'
于2003年05月27日17:05 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif'
用于打印DATAGRID控件中的数据. 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif'
======================================= 
img_a6339ee3e57d1d52bc7d02b338e15a60.gif

img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
Imports  System.Drawing.Printing
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
Imports  System.Drawing.Color
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
Imports  System.Windows.Forms
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
Imports  System.Drawing.Font
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
Imports  System.Drawing.PointF
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
Imports  System.Windows.Forms.DataGrid
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
Imports  System.Drawing.Pen
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
Imports  System.Drawing
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif
Public   Class Print Class Print
img_33d02437d135341f0800e3d415312ae8.gif    
'用户可自定义 
img_33d02437d135341f0800e3d415312ae8.gif
    Private TableFont As New Font("宋体"9)           '当前要打印文本的字体及字号 
img_33d02437d135341f0800e3d415312ae8.gif
    Private HeadFont As New Font("黑体"12, FontStyle.Underline) '表头字体 
img_33d02437d135341f0800e3d415312ae8.gif
    Private SubHeadFont As New Font("楷体_GB2312"10, FontStyle.Regular) '副表头字体 
img_33d02437d135341f0800e3d415312ae8.gif
    Private HeadText As String   '表头文字 
img_33d02437d135341f0800e3d415312ae8.gif
    Private SubHeadLeftText As String  '副表头左文字 
img_33d02437d135341f0800e3d415312ae8.gif
    Private SubHeadRightText As String  '副表头右文字 
img_33d02437d135341f0800e3d415312ae8.gif
    Private HeadHeight As Integer = 40 '表头高度 
img_33d02437d135341f0800e3d415312ae8.gif
    Private SubHeadHeight As Integer = 30 '副表头高度 
img_33d02437d135341f0800e3d415312ae8.gif
    Private FootFont As New Font("黑体"12, FontStyle.Underline) '表脚字体 
img_33d02437d135341f0800e3d415312ae8.gif
    Private SubFootFont As New Font("楷体_GB2312"10, FontStyle.Regular) '副表脚字体 
img_33d02437d135341f0800e3d415312ae8.gif
    Private FootText As String  '表脚文字 
img_33d02437d135341f0800e3d415312ae8.gif
    Private SubFootLeftText As String  '副表脚左文字 
img_33d02437d135341f0800e3d415312ae8.gif
    Private SubfootRightText As String
img_33d02437d135341f0800e3d415312ae8.gif    
Private FootHeight As Integer = 40 '表脚高度 
img_33d02437d135341f0800e3d415312ae8.gif
    Private SubFootHeight As Integer = 30 '副表脚高度 
img_33d02437d135341f0800e3d415312ae8.gif
    Dim X_unit As Integer                               '表的基本单位 
img_33d02437d135341f0800e3d415312ae8.gif
    Dim Y_unit As Integer = TableFont.Height * 2.5
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif    
'以下为模块内部使用 
img_33d02437d135341f0800e3d415312ae8.gif
    Private ev As PrintPageEventArgs
img_33d02437d135341f0800e3d415312ae8.gif    
Private PrintDocument1 As PrintDocument
img_33d02437d135341f0800e3d415312ae8.gif    
Private DataGridColumn As DataColumn
img_33d02437d135341f0800e3d415312ae8.gif    
Private DataGridRow As DataRow
img_33d02437d135341f0800e3d415312ae8.gif    
Private DataTable1 As DataTable
img_33d02437d135341f0800e3d415312ae8.gif    
Private Cols As Integer                             '当前要打印的列 
img_33d02437d135341f0800e3d415312ae8.gif
    Private Rows As Integer = 0                         '当前要打印的行 
img_33d02437d135341f0800e3d415312ae8.gif
    Private ColsCount As Integer                        '当前DATAGRID共有多少列 
img_33d02437d135341f0800e3d415312ae8.gif
    Private PrintingLineNumber As Integer = 0           '当前正要打印的行号 
img_33d02437d135341f0800e3d415312ae8.gif
    Private PageRecordNumber As Integer                 '当前要所要打印的记录行数,由计算得到. 
img_33d02437d135341f0800e3d415312ae8.gif
    Private PrintingPageNumber As Integer = 0           '正要打印的页号 
img_33d02437d135341f0800e3d415312ae8.gif
    Private PageNumber As Integer                       '共需要打印的页数 
img_33d02437d135341f0800e3d415312ae8.gif
    Private PrintRecordLeave As Integer                 '当前还有多少页没有打印 
img_33d02437d135341f0800e3d415312ae8.gif
    Private PrintRecordComplete As Integer = 0         '已经打印完的记录数 
img_33d02437d135341f0800e3d415312ae8.gif
    Private Pleft As Integer
img_33d02437d135341f0800e3d415312ae8.gif    
Private Ptop As Integer
img_33d02437d135341f0800e3d415312ae8.gif    
Private Pright As Integer
img_33d02437d135341f0800e3d415312ae8.gif    
Private Pbottom As Integer
img_33d02437d135341f0800e3d415312ae8.gif    
Private Pwidth As Integer
img_33d02437d135341f0800e3d415312ae8.gif    
Private Pheigh As Integer
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif    
Private DrawBrush As New SolidBrush(System.Drawing.Color.Black)     '当前画笔颜色 
img_33d02437d135341f0800e3d415312ae8.gif
    Private PrintRecordNumber As Integer '每页打印的记录条数 
img_33d02437d135341f0800e3d415312ae8.gif
    Private Totalpage As Integer '总共应该打印的页数 
img_33d02437d135341f0800e3d415312ae8.gif

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
Sub New()Sub New(ByVal TableSource As DataTable)
img_33d02437d135341f0800e3d415312ae8.gif        DataTable1 
= New DataTable
img_33d02437d135341f0800e3d415312ae8.gif        DataTable1 
= TableSource
img_33d02437d135341f0800e3d415312ae8.gif        ColsCount 
= DataTable1.Columns.Count
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
End Sub

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif    
'用户自定义字体及字号 
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif
    Public WriteOnly Property SetTableFont()Property SetTableFont() As System.Drawing.Font
img_33d02437d135341f0800e3d415312ae8.gif        
Set(ByVal Value As System.Drawing.Font)
img_33d02437d135341f0800e3d415312ae8.gif            TableFont 
= Value
img_33d02437d135341f0800e3d415312ae8.gif        
End Set
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
End Property

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
Public WriteOnly Property SetHeadFont()Property SetHeadFont() As System.Drawing.Font
img_33d02437d135341f0800e3d415312ae8.gif        
Set(ByVal Value As System.Drawing.Font)
img_33d02437d135341f0800e3d415312ae8.gif            HeadFont 
= Value
img_33d02437d135341f0800e3d415312ae8.gif        
End Set
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
End Property

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
Public WriteOnly Property SetSubHeadFont()Property SetSubHeadFont() As System.Drawing.Font
img_33d02437d135341f0800e3d415312ae8.gif        
Set(ByVal Value As System.Drawing.Font)
img_33d02437d135341f0800e3d415312ae8.gif            SubHeadFont 
= Value
img_33d02437d135341f0800e3d415312ae8.gif        
End Set
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
End Property

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
Public WriteOnly Property SetFootFont()Property SetFootFont() As System.Drawing.Font
img_33d02437d135341f0800e3d415312ae8.gif        
Set(ByVal Value As System.Drawing.Font)
img_33d02437d135341f0800e3d415312ae8.gif            FootFont 
= Value
img_33d02437d135341f0800e3d415312ae8.gif        
End Set
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
End Property

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
Public WriteOnly Property SetSubFootFont()Property SetSubFootFont() As System.Drawing.Font
img_33d02437d135341f0800e3d415312ae8.gif        
Set(ByVal Value As System.Drawing.Font)
img_33d02437d135341f0800e3d415312ae8.gif            SubFootFont 
= Value
img_33d02437d135341f0800e3d415312ae8.gif        
End Set
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
End Property

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
Public WriteOnly Property SetHeadText()Property SetHeadText() As String
img_33d02437d135341f0800e3d415312ae8.gif        
Set(ByVal Value As String)
img_33d02437d135341f0800e3d415312ae8.gif            HeadText 
= Value
img_33d02437d135341f0800e3d415312ae8.gif        
End Set
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
End Property

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
Public WriteOnly Property SetSubHeadLeftText()Property SetSubHeadLeftText() As String
img_33d02437d135341f0800e3d415312ae8.gif        
Set(ByVal Value As String)
img_33d02437d135341f0800e3d415312ae8.gif            SubHeadLeftText 
= Value
img_33d02437d135341f0800e3d415312ae8.gif        
End Set
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
End Property

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
Public WriteOnly Property SetSubHeadRightText()Property SetSubHeadRightText() As String
img_33d02437d135341f0800e3d415312ae8.gif        
Set(ByVal Value As String)
img_33d02437d135341f0800e3d415312ae8.gif            SubHeadRightText 
= Value
img_33d02437d135341f0800e3d415312ae8.gif        
End Set
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
End Property

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
Public WriteOnly Property SetFootText()Property SetFootText() As String
img_33d02437d135341f0800e3d415312ae8.gif        
Set(ByVal Value As String)
img_33d02437d135341f0800e3d415312ae8.gif            FootText 
= Value
img_33d02437d135341f0800e3d415312ae8.gif        
End Set
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
End Property

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
Public WriteOnly Property SetSubFootLeftText()Property SetSubFootLeftText() As String
img_33d02437d135341f0800e3d415312ae8.gif        
Set(ByVal Value As String)
img_33d02437d135341f0800e3d415312ae8.gif            SubFootLeftText 
= Value
img_33d02437d135341f0800e3d415312ae8.gif        
End Set
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
End Property

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
Public WriteOnly Property SetSubFootRightText()Property SetSubFootRightText() As String
img_33d02437d135341f0800e3d415312ae8.gif        
Set(ByVal Value As String)
img_33d02437d135341f0800e3d415312ae8.gif            SubfootRightText 
= Value
img_33d02437d135341f0800e3d415312ae8.gif        
End Set
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
End Property

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
Public WriteOnly Property SetHeadHeight()Property SetHeadHeight() As Integer
img_33d02437d135341f0800e3d415312ae8.gif        
Set(ByVal Value As Integer)
img_33d02437d135341f0800e3d415312ae8.gif            HeadHeight 
= Value
img_33d02437d135341f0800e3d415312ae8.gif        
End Set
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
End Property

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
Public WriteOnly Property SetSubHeadHeight()Property SetSubHeadHeight() As Integer
img_33d02437d135341f0800e3d415312ae8.gif        
Set(ByVal Value As Integer)
img_33d02437d135341f0800e3d415312ae8.gif            SubHeadHeight 
= Value
img_33d02437d135341f0800e3d415312ae8.gif        
End Set
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
End Property

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
Public WriteOnly Property SetFootHeight()Property SetFootHeight() As Integer
img_33d02437d135341f0800e3d415312ae8.gif        
Set(ByVal Value As Integer)
img_33d02437d135341f0800e3d415312ae8.gif            FootHeight 
= Value
img_33d02437d135341f0800e3d415312ae8.gif        
End Set
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
End Property

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
Public WriteOnly Property SetSubFootHeight()Property SetSubFootHeight() As Integer
img_33d02437d135341f0800e3d415312ae8.gif        
Set(ByVal Value As Integer)
img_33d02437d135341f0800e3d415312ae8.gif            SubFootHeight 
= Value
img_33d02437d135341f0800e3d415312ae8.gif        
End Set
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
End Property

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
Public WriteOnly Property SetCellHeight()Property SetCellHeight() As Integer
img_33d02437d135341f0800e3d415312ae8.gif        
Set(ByVal Value As Integer)
img_33d02437d135341f0800e3d415312ae8.gif            Y_unit 
= Value
img_33d02437d135341f0800e3d415312ae8.gif        
End Set
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
End Property

img_33d02437d135341f0800e3d415312ae8.gif
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
Public Sub Print()Sub Print()
img_33d02437d135341f0800e3d415312ae8.gif        
Try
img_33d02437d135341f0800e3d415312ae8.gif            PrintDocument1 
= New Printing.PrintDocument
img_33d02437d135341f0800e3d415312ae8.gif            
AddHandler PrintDocument1.PrintPage, AddressOf Me.PrintDocument1_PrintPage
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif            
Dim PageSetup As PageSetupDialog
img_33d02437d135341f0800e3d415312ae8.gif            PageSetup 
= New PageSetupDialog
img_33d02437d135341f0800e3d415312ae8.gif            PageSetup.Document 
= PrintDocument1
img_33d02437d135341f0800e3d415312ae8.gif            PrintDocument1.DefaultPageSettings 
= PageSetup.PageSettings
img_33d02437d135341f0800e3d415312ae8.gif            
If PageSetup.ShowDialog() = DialogResult.Cancel Then
img_33d02437d135341f0800e3d415312ae8.gif                
Exit Sub
img_33d02437d135341f0800e3d415312ae8.gif            
End If
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif            Pleft 
= PrintDocument1.DefaultPageSettings.Margins.Left
img_33d02437d135341f0800e3d415312ae8.gif            Ptop 
= PrintDocument1.DefaultPageSettings.Margins.Top
img_33d02437d135341f0800e3d415312ae8.gif            Pright 
= PrintDocument1.DefaultPageSettings.Margins.Right
img_33d02437d135341f0800e3d415312ae8.gif            Pbottom 
= PrintDocument1.DefaultPageSettings.Margins.Bottom
img_33d02437d135341f0800e3d415312ae8.gif            Pwidth 
= PrintDocument1.DefaultPageSettings.Bounds.Width
img_33d02437d135341f0800e3d415312ae8.gif            Pheigh 
= PrintDocument1.DefaultPageSettings.Bounds.Height
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif            
'将当前页分成基本的单元 
img_33d02437d135341f0800e3d415312ae8.gif
            X_unit = (Pwidth - Pleft - Pright) / DataTable1.Columns.Count - 1
img_33d02437d135341f0800e3d415312ae8.gif            PrintRecordNumber 
= (Pheigh - Ptop - Pbottom - HeadHeight - SubHeadHeight - FootHeight - SubFootHeight - Y_unit) \ Y_unit
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif            
If DataTable1.Rows.Count > PrintRecordNumber Then
img_33d02437d135341f0800e3d415312ae8.gif                
If DataTable1.Rows.Count Mod PrintRecordNumber = 0 Then
img_33d02437d135341f0800e3d415312ae8.gif                    Totalpage 
= DataTable1.Rows.Count \ PrintRecordNumber
img_33d02437d135341f0800e3d415312ae8.gif                
Else
img_33d02437d135341f0800e3d415312ae8.gif                    Totalpage 
= DataTable1.Rows.Count \ PrintRecordNumber + 1
img_33d02437d135341f0800e3d415312ae8.gif                
End If
img_33d02437d135341f0800e3d415312ae8.gif            
Else
img_33d02437d135341f0800e3d415312ae8.gif                Totalpage 
= 1
img_33d02437d135341f0800e3d415312ae8.gif            
End If
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif            PrintDocument1.DocumentName 
= Totalpage.ToString
img_33d02437d135341f0800e3d415312ae8.gif            
Dim PrintPriview As PrintPreviewDialog
img_33d02437d135341f0800e3d415312ae8.gif            PrintPriview 
= New PrintPreviewDialog
img_33d02437d135341f0800e3d415312ae8.gif            PrintPriview.Document 
= PrintDocument1
img_33d02437d135341f0800e3d415312ae8.gif            PrintPriview.WindowState 
= FormWindowState.Maximized
img_33d02437d135341f0800e3d415312ae8.gif            PrintPriview.ShowDialog()
img_33d02437d135341f0800e3d415312ae8.gif        
Catch ex As Exception
img_33d02437d135341f0800e3d415312ae8.gif            
MsgBox("打印错误,请检查打印设置!"16"错误")
img_33d02437d135341f0800e3d415312ae8.gif        
End Try
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
End Sub

img_33d02437d135341f0800e3d415312ae8.gif
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
Private Sub PrintDocument1_PrintPage()Sub PrintDocument1_PrintPage(ByVal sender As ObjectByVal ev As System.Drawing.Printing.PrintPageEventArgs)
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        
Dim row_count As Integer '当前要打印的行 
img_33d02437d135341f0800e3d415312ae8.gif
        PrintRecordLeave = DataTable1.Rows.Count - PrintRecordComplete '还有多少条记录没有打印 
img_33d02437d135341f0800e3d415312ae8.gif
        If PrintRecordLeave > 0 Then
img_33d02437d135341f0800e3d415312ae8.gif            
If PrintRecordLeave Mod PrintRecordNumber = 0 Then
img_33d02437d135341f0800e3d415312ae8.gif                PageNumber 
= PrintRecordLeave \ PrintRecordNumber
img_33d02437d135341f0800e3d415312ae8.gif            
Else
img_33d02437d135341f0800e3d415312ae8.gif                PageNumber 
= PrintRecordLeave \ PrintRecordNumber + 1
img_33d02437d135341f0800e3d415312ae8.gif            
End If
img_33d02437d135341f0800e3d415312ae8.gif        
Else
img_33d02437d135341f0800e3d415312ae8.gif            PageNumber 
= 0
img_33d02437d135341f0800e3d415312ae8.gif        
End If
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        
'正在打印的页数 
img_33d02437d135341f0800e3d415312ae8.gif
        PrintingPageNumber = 0       '因为每打印一个新页都要计算还有多少页没有打印所以以打印的页数初始为0 
img_33d02437d135341f0800e3d415312ae8.gif
        '计算,余下的记录条数是否还可以在一页打印,不满一页时为假 
img_33d02437d135341f0800e3d415312ae8.gif
        If DataTable1.Rows.Count - PrintingPageNumber * PrintRecordNumber >= PrintRecordNumber Then
img_33d02437d135341f0800e3d415312ae8.gif            PageRecordNumber 
= PrintRecordNumber
img_33d02437d135341f0800e3d415312ae8.gif        
Else
img_33d02437d135341f0800e3d415312ae8.gif            PageRecordNumber 
= (DataTable1.Rows.Count - PrintingPageNumber * PrintRecordNumber) Mod PrintRecordNumber
img_33d02437d135341f0800e3d415312ae8.gif        
End If
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        
Dim fmt As New StringFormat
img_33d02437d135341f0800e3d415312ae8.gif        fmt.LineAlignment 
= StringAlignment.Center '上下对齐 
img_33d02437d135341f0800e3d415312ae8.gif
        fmt.FormatFlags = StringFormatFlags.LineLimit '自动换行 
img_33d02437d135341f0800e3d415312ae8.gif

img_33d02437d135341f0800e3d415312ae8.gif        
Dim Rect As New Rectangle '打印区域 
img_33d02437d135341f0800e3d415312ae8.gif
        Dim Pen As New Pen(Brushes.Black, 1'打印表格线格式 
img_33d02437d135341f0800e3d415312ae8.gif

img_33d02437d135341f0800e3d415312ae8.gif        
While PrintingPageNumber <= PageNumber
img_33d02437d135341f0800e3d415312ae8.gif            fmt.Alignment 
= StringAlignment.Center '表头中间对齐 
img_33d02437d135341f0800e3d415312ae8.gif
            Rect.Width = Pwidth - Pleft - Pright '表头和副表头宽度等于设置区域宽度 
img_33d02437d135341f0800e3d415312ae8.gif
            Rect.Height = HeadHeight
img_33d02437d135341f0800e3d415312ae8.gif            Rect.X 
= Pleft
img_33d02437d135341f0800e3d415312ae8.gif            Rect.Y 
= Ptop
img_33d02437d135341f0800e3d415312ae8.gif            ev.Graphics.DrawString(HeadText, HeadFont, Brushes.Black, RectangleF.op_Implicit(Rect), fmt) 
'打印表头 
img_33d02437d135341f0800e3d415312ae8.gif

img_33d02437d135341f0800e3d415312ae8.gif            fmt.Alignment 
= StringAlignment.Near  '副表头左对齐 
img_33d02437d135341f0800e3d415312ae8.gif
            Rect.Width = (Pwidth - Pleft - Pright) / 2 - 1
img_33d02437d135341f0800e3d415312ae8.gif            Rect.Height 
= SubHeadHeight
img_33d02437d135341f0800e3d415312ae8.gif            Rect.Y 
= Ptop + HeadHeight
img_33d02437d135341f0800e3d415312ae8.gif            ev.Graphics.DrawString(SubHeadLeftText, SubHeadFont, Brushes.Black, RectangleF.op_Implicit(Rect), fmt) 
'打印副表头左 
img_33d02437d135341f0800e3d415312ae8.gif

img_33d02437d135341f0800e3d415312ae8.gif            fmt.FormatFlags 
= StringFormatFlags.DirectionRightToLeft '右副表头文字从右往左排列 
img_33d02437d135341f0800e3d415312ae8.gif
            fmt.Alignment = StringAlignment.Near    '右副表头右对齐 
img_33d02437d135341f0800e3d415312ae8.gif
            Rect.X = Pleft + (Pwidth - Pleft - Pright) / 2
img_33d02437d135341f0800e3d415312ae8.gif            ev.Graphics.DrawString(SubHeadRightText, SubHeadFont, Brushes.Black, RectangleF.op_Implicit(Rect), fmt) 
'打印副表头右 
img_33d02437d135341f0800e3d415312ae8.gif

img_33d02437d135341f0800e3d415312ae8.gif            fmt.Alignment 
= StringAlignment.Center
img_33d02437d135341f0800e3d415312ae8.gif            Rect.X 
= Pleft
img_33d02437d135341f0800e3d415312ae8.gif            Rect.Y 
= Ptop + HeadHeight + SubHeadHeight + (PrintRecordNumber + 1* (Y_unit) + SubFootHeight
img_33d02437d135341f0800e3d415312ae8.gif            Rect.Height 
= FootHeight
img_33d02437d135341f0800e3d415312ae8.gif            Rect.Width 
= Pwidth - Pleft - Pright
img_33d02437d135341f0800e3d415312ae8.gif            ev.Graphics.DrawString(FootText, FootFont, Brushes.Black, RectangleF.op_Implicit(Rect), fmt)   
'打印表脚 
img_33d02437d135341f0800e3d415312ae8.gif

img_33d02437d135341f0800e3d415312ae8.gif            fmt.Alignment 
= StringAlignment.Far   '副表左左对齐 
img_33d02437d135341f0800e3d415312ae8.gif
            Rect.X = Pleft
img_33d02437d135341f0800e3d415312ae8.gif            Rect.Y 
= Ptop + HeadHeight + SubHeadHeight + (PrintRecordNumber + 1* (Y_unit)
img_33d02437d135341f0800e3d415312ae8.gif            Rect.Height 
= SubFootHeight
img_33d02437d135341f0800e3d415312ae8.gif            Rect.Width 
= (Pwidth - Pleft - Pright) / 2 - 1
img_33d02437d135341f0800e3d415312ae8.gif            ev.Graphics.DrawString(SubFootLeftText, SubFootFont, Brushes.Black, RectangleF.op_Implicit(Rect), fmt) 
'打印左表脚 
img_33d02437d135341f0800e3d415312ae8.gif

img_33d02437d135341f0800e3d415312ae8.gif            fmt.Alignment 
= StringAlignment.Near   '副表头右对齐 
img_33d02437d135341f0800e3d415312ae8.gif
            Rect.X = Pleft + (Pwidth - Pleft - Pright) / 2
img_33d02437d135341f0800e3d415312ae8.gif            
If DataTable1.Rows.Count = 0 Then
img_33d02437d135341f0800e3d415312ae8.gif                SubfootRightText 
= "" & Totalpage & "页,共" & Totalpage & ""
img_33d02437d135341f0800e3d415312ae8.gif            
Else
img_33d02437d135341f0800e3d415312ae8.gif                SubfootRightText 
= "" & Totalpage - PageNumber + 1 & "页,共" & Totalpage & ""
img_33d02437d135341f0800e3d415312ae8.gif            
End If
img_33d02437d135341f0800e3d415312ae8.gif            ev.Graphics.DrawString(SubfootRightText, SubFootFont, Brushes.Black, RectangleF.op_Implicit(Rect), fmt) 
'打印右表脚 
img_33d02437d135341f0800e3d415312ae8.gif

img_33d02437d135341f0800e3d415312ae8.gif            
'得到datatable的所有列名 
img_33d02437d135341f0800e3d415312ae8.gif
            fmt.Alignment = StringAlignment.Center
img_33d02437d135341f0800e3d415312ae8.gif            
Dim ColumnText(DataTable1.Columns.Count) As String
img_33d02437d135341f0800e3d415312ae8.gif            
Dim Table As Integer
img_33d02437d135341f0800e3d415312ae8.gif            
For Cols = 0 To DataTable1.Columns.Count - 1
img_33d02437d135341f0800e3d415312ae8.gif                ColumnText(Cols) 
= DataTable1.Columns(Cols).ToString         '得到当前所有的列名 
img_33d02437d135341f0800e3d415312ae8.gif
                Rect.X = Pleft + X_unit * Cols
img_33d02437d135341f0800e3d415312ae8.gif                Rect.Y 
= Ptop + HeadHeight + SubHeadHeight
img_33d02437d135341f0800e3d415312ae8.gif                Rect.Width 
= X_unit
img_33d02437d135341f0800e3d415312ae8.gif                Rect.Height 
= Y_unit
img_33d02437d135341f0800e3d415312ae8.gif                ev.Graphics.DrawString(ColumnText(Cols), 
New Font(TableFont, FontStyle.Bold), DrawBrush, RectangleF.op_Implicit(Rect), fmt)
img_33d02437d135341f0800e3d415312ae8.gif                ev.Graphics.DrawRectangle(Pen, Rect)
img_33d02437d135341f0800e3d415312ae8.gif            
Next
img_33d02437d135341f0800e3d415312ae8.gif            
'结束---------------------得到datatable的所有列名 
img_33d02437d135341f0800e3d415312ae8.gif
            Dim PrintingLine As Integer = 0                         '当前页面已经打印的记录行数 
img_33d02437d135341f0800e3d415312ae8.gif
            While PrintingLine < PageRecordNumber
img_33d02437d135341f0800e3d415312ae8.gif                DataGridRow 
= DataTable1.Rows(PrintRecordComplete)        '确定要当前要打印的记录的行号 
img_33d02437d135341f0800e3d415312ae8.gif
                For Cols = 0 To DataTable1.Columns.Count - 1
img_33d02437d135341f0800e3d415312ae8.gif                    Rect.X 
= Pleft + X_unit * Cols
img_33d02437d135341f0800e3d415312ae8.gif                    Rect.Y 
= Ptop + HeadHeight + SubHeadHeight + (PrintingLine + 1* (Y_unit)
img_33d02437d135341f0800e3d415312ae8.gif                    Rect.Width 
= X_unit
img_33d02437d135341f0800e3d415312ae8.gif                    Rect.Height 
= Y_unit
img_33d02437d135341f0800e3d415312ae8.gif                    
If DataGridRow(ColumnText(Cols)) Is System.DBNull.Value = False Then
img_33d02437d135341f0800e3d415312ae8.gif                        ev.Graphics.DrawString(DataGridRow(ColumnText(Cols)), TableFont, DrawBrush, RectangleF.op_Implicit(Rect), fmt)
img_33d02437d135341f0800e3d415312ae8.gif                    
End If
img_33d02437d135341f0800e3d415312ae8.gif                    ev.Graphics.DrawRectangle(Pen, Rect)
img_33d02437d135341f0800e3d415312ae8.gif                
Next
img_33d02437d135341f0800e3d415312ae8.gif                PrintingLine 
+= 1
img_33d02437d135341f0800e3d415312ae8.gif                PrintRecordComplete 
+= 1
img_33d02437d135341f0800e3d415312ae8.gif                
If PrintRecordComplete >= DataTable1.Rows.Count Then
img_33d02437d135341f0800e3d415312ae8.gif                    ev.HasMorePages 
= False
img_33d02437d135341f0800e3d415312ae8.gif                    PrintRecordComplete 
= 0
img_33d02437d135341f0800e3d415312ae8.gif                    
Exit Sub
img_33d02437d135341f0800e3d415312ae8.gif                
End If
img_33d02437d135341f0800e3d415312ae8.gif            
End While
img_33d02437d135341f0800e3d415312ae8.gif            PrintingPageNumber 
+= 1
img_33d02437d135341f0800e3d415312ae8.gif            
If PrintingPageNumber >= PageNumber Then
img_33d02437d135341f0800e3d415312ae8.gif                ev.HasMorePages 
= False
img_33d02437d135341f0800e3d415312ae8.gif            
Else
img_33d02437d135341f0800e3d415312ae8.gif                ev.HasMorePages 
= True
img_33d02437d135341f0800e3d415312ae8.gif                
Exit While
img_33d02437d135341f0800e3d415312ae8.gif            
End If
img_33d02437d135341f0800e3d415312ae8.gif        
End While
img_33d02437d135341f0800e3d415312ae8.gif
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
End Sub

img_33d02437d135341f0800e3d415312ae8.gif
img_05dd8d549cff04457a6366b0a7c9352a.gif
End Class

img_a6339ee3e57d1d52bc7d02b338e15a60.gif
相关文章
|
IDE API 开发工具
拦截|篡改|伪造.NET类库中不限于public的类和方法
本文除了回顾拦截.NET类库中的方法,实现方法参数的篡改、方法返回结果的伪造,再着重介绍.NET类库中非public类及方法如何拦截。
拦截|篡改|伪造.NET类库中不限于public的类和方法
|
1月前
|
开发框架 .NET C#
C#|.net core 基础 - 删除字符串最后一个字符的七大类N种实现方式
【10月更文挑战第9天】在 C#/.NET Core 中,有多种方法可以删除字符串的最后一个字符,包括使用 `Substring` 方法、`Remove` 方法、`ToCharArray` 与 `Array.Copy`、`StringBuilder`、正则表达式、循环遍历字符数组以及使用 LINQ 的 `SkipLast` 方法。
|
14天前
.NET 4.0下实现.NET4.5的Task类相似功能组件
【10月更文挑战第29天】在.NET 4.0 环境下,可以使用 `BackgroundWorker` 类来实现类似于 .NET 4.5 中 `Task` 类的功能。`BackgroundWorker` 允许在后台执行耗时操作,同时不会阻塞用户界面线程,并支持进度报告和取消操作。尽管它有一些局限性,如复杂的事件处理模型和不灵活的任务管理方式,但在某些情况下仍能有效替代 `Task` 类。
|
1月前
|
API
使用`System.Net.WebClient`类发送HTTP请求来调用阿里云短信API
使用`System.Net.WebClient`类发送HTTP请求来调用阿里云短信API
21 0
|
3月前
|
缓存 程序员
封装一个给 .NET Framework 用的内存缓存帮助类
封装一个给 .NET Framework 用的内存缓存帮助类
|
5月前
|
存储 Go C#
【.NET Core】深入理解IO之File类
【.NET Core】深入理解IO之File类
75 6
|
5月前
|
存储 开发框架 缓存
【.NET Core】你真的了解HttpRuntime类吗
【.NET Core】你真的了解HttpRuntime类吗
46 0
|
6月前
|
消息中间件
.NET 中 Channel 类简单使用
`System.Threading.Channels` 提供异步生产者-消费者数据结构,用于.NET Standard上的跨平台同步。频道实现生产者/消费者模型,允许在任务间异步传递数据。简单示例展示如何创建无界和有界频道,以及多生产者和消费者共享频道的场景。频道常用于内存中的消息队列,通过控制生产者和消费者的速率来调整系统流量。
|
6月前
|
SQL 数据库连接 数据库
VB.NET 中使用SqlConnection类连接到Microsoft SQL Server数据库的详细步骤
VB.NET 中使用SqlConnection类连接到Microsoft SQL Server数据库的详细步骤
290 0
|
开发框架 前端开发 .NET
ASP.NET Core 基础知识之​Startup 类配置
Startup 类配置服务和应用的请求管道。
151 0