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

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

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

ExpandedSubBlockStart.gifContractedSubBlock.gif    
Sub New()Sub New(ByVal TableSource As DataTable)
InBlock.gif        DataTable1 
= New DataTable
InBlock.gif        DataTable1 
= TableSource
InBlock.gif        ColsCount 
= DataTable1.Columns.Count
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
InBlock.gif    
'用户自定义字体及字号 
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public WriteOnly Property SetTableFont()Property SetTableFont() As System.Drawing.Font
InBlock.gif        
Set(ByVal Value As System.Drawing.Font)
InBlock.gif            TableFont 
= Value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public WriteOnly Property SetHeadFont()Property SetHeadFont() As System.Drawing.Font
InBlock.gif        
Set(ByVal Value As System.Drawing.Font)
InBlock.gif            HeadFont 
= Value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public WriteOnly Property SetSubHeadFont()Property SetSubHeadFont() As System.Drawing.Font
InBlock.gif        
Set(ByVal Value As System.Drawing.Font)
InBlock.gif            SubHeadFont 
= Value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public WriteOnly Property SetFootFont()Property SetFootFont() As System.Drawing.Font
InBlock.gif        
Set(ByVal Value As System.Drawing.Font)
InBlock.gif            FootFont 
= Value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public WriteOnly Property SetSubFootFont()Property SetSubFootFont() As System.Drawing.Font
InBlock.gif        
Set(ByVal Value As System.Drawing.Font)
InBlock.gif            SubFootFont 
= Value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public WriteOnly Property SetHeadText()Property SetHeadText() As String
InBlock.gif        
Set(ByVal Value As String)
InBlock.gif            HeadText 
= Value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public WriteOnly Property SetSubHeadLeftText()Property SetSubHeadLeftText() As String
InBlock.gif        
Set(ByVal Value As String)
InBlock.gif            SubHeadLeftText 
= Value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public WriteOnly Property SetSubHeadRightText()Property SetSubHeadRightText() As String
InBlock.gif        
Set(ByVal Value As String)
InBlock.gif            SubHeadRightText 
= Value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public WriteOnly Property SetFootText()Property SetFootText() As String
InBlock.gif        
Set(ByVal Value As String)
InBlock.gif            FootText 
= Value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public WriteOnly Property SetSubFootLeftText()Property SetSubFootLeftText() As String
InBlock.gif        
Set(ByVal Value As String)
InBlock.gif            SubFootLeftText 
= Value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public WriteOnly Property SetSubFootRightText()Property SetSubFootRightText() As String
InBlock.gif        
Set(ByVal Value As String)
InBlock.gif            SubfootRightText 
= Value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public WriteOnly Property SetHeadHeight()Property SetHeadHeight() As Integer
InBlock.gif        
Set(ByVal Value As Integer)
InBlock.gif            HeadHeight 
= Value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public WriteOnly Property SetSubHeadHeight()Property SetSubHeadHeight() As Integer
InBlock.gif        
Set(ByVal Value As Integer)
InBlock.gif            SubHeadHeight 
= Value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public WriteOnly Property SetFootHeight()Property SetFootHeight() As Integer
InBlock.gif        
Set(ByVal Value As Integer)
InBlock.gif            FootHeight 
= Value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public WriteOnly Property SetSubFootHeight()Property SetSubFootHeight() As Integer
InBlock.gif        
Set(ByVal Value As Integer)
InBlock.gif            SubFootHeight 
= Value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public WriteOnly Property SetCellHeight()Property SetCellHeight() As Integer
InBlock.gif        
Set(ByVal Value As Integer)
InBlock.gif            Y_unit 
= Value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

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

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

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

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

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

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

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

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

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

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

InBlock.gif
ExpandedBlockEnd.gif
End Class

None.gif

转载于:https://www.cnblogs.com/aowind/archive/2005/12/14/297141.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值