`
灵雨飘零
  • 浏览: 34987 次
  • 性别: Icon_minigender_1
  • 来自: 唐山
文章分类
社区版块
存档分类
最新评论

DataGridView打印

 
阅读更多

Public Class dayreport1
Inherits System.Windows.Forms.Form

Private TableFont As New Font("宋体", 9) '当前要打印文本的字体及字号
Private HeadFont As New Font("黑体", 12, FontStyle.Underline) '表头字体
Private SubHeadFont As New Font("楷体_GB2312", 10, FontStyle.Regular) '副表头字体
Private HeadText As String '表头文字
Private SubHeadLeftText As String '副表头左文字
Private SubHeadRightText As String '副表头右文字
Private HeadHeight As Integer = 40 '表头高度
Private SubHeadHeight As Integer = 30 '副表头高度
Private FootFont As New Font("黑体", 12, FontStyle.Underline) '表脚字体
Private SubFootFont As New Font("楷体_GB2312", 10, FontStyle.Regular) '副表脚字体
Private FootText As String '表脚文字
Private SubFootLeftText As String '副表脚左文字
Private SubfootRightText As String
Private FootHeight As Integer = 40 '表脚高度
Private SubFootHeight As Integer = 30 '副表脚高度
Dim X_unit As Integer '表的基本单位
Dim Y_unit As Integer = TableFont.Height * 2.5

'以下为模块内部使用
Private ev As Printing.PrintPageEventArgs
Private DataGridColumn As DataColumn
Private DataGridRow As DataRow
Private DataTable1 As DataTable
Private Cols As Integer '当前要打印的列
Private Rows As Integer = 0 '当前要打印的行
Private ColsCount As Integer '当前DATAGRID共有多少列
Private PrintingLineNumber As Integer = 0 '当前正要打印的行号
Private PageRecordNumber As Integer '当前要所要打印的记录行数,由计算得到.
Private PrintingPageNumber As Integer = 0 '正要打印的页号
Private PageNumber As Integer '共需要打印的页数
Private PrintRecordLeave As Integer '当前还有多少页没有打印
Private PrintRecordComplete As Integer = 0 '已经打印完的记录数
Private Pleft As Integer
Private Ptop As Integer
Private Pright As Integer
Private Pbottom As Integer
Private Pwidth As Integer
Private Pheigh As Integer

Private DrawBrush As New SolidBrush(System.Drawing.Color.Black) '当前画笔颜色
Private PrintRecordNumber As Integer '每页打印的记录条数
Private Totalpage As Integer '总共应该打印的页数
'用户自定义字体及字号
Public WriteOnly Property SetTableFont() As System.Drawing.Font
Set(ByVal Value As System.Drawing.Font)
TableFont = Value
End Set
End Property
Public WriteOnly Property SetHeadFont() As System.Drawing.Font
Set(ByVal Value As System.Drawing.Font)
HeadFont = Value
End Set
End Property
Public WriteOnly Property SetSubHeadFont() As System.Drawing.Font
Set(ByVal Value As System.Drawing.Font)
SubHeadFont = Value
End Set
End Property
Public WriteOnly Property SetFootFont() As System.Drawing.Font
Set(ByVal Value As System.Drawing.Font)
FootFont = Value
End Set
End Property
Public WriteOnly Property SetSubFootFont() As System.Drawing.Font
Set(ByVal Value As System.Drawing.Font)
SubFootFont = Value
End Set
End Property
Public WriteOnly Property SetHeadText() As String
Set(ByVal Value As String)
HeadText = Value
End Set
End Property
Public WriteOnly Property SetSubHeadLeftText() As String
Set(ByVal Value As String)
SubHeadLeftText = Value
End Set
End Property
Public WriteOnly Property SetSubHeadRightText() As String
Set(ByVal Value As String)
SubHeadRightText = Value
End Set
End Property
Public WriteOnly Property SetFootText() As String
Set(ByVal Value As String)
FootText = Value
End Set
End Property
Public WriteOnly Property SetSubFootLeftText() As String
Set(ByVal Value As String)
SubFootLeftText = Value
End Set
End Property
Public WriteOnly Property SetSubFootRightText() As String
Set(ByVal Value As String)
SubfootRightText = Value
End Set
End Property
Public WriteOnly Property SetHeadHeight() As Integer
Set(ByVal Value As Integer)
HeadHeight = Value
End Set
End Property
Public WriteOnly Property SetSubHeadHeight() As Integer
Set(ByVal Value As Integer)
SubHeadHeight = Value
End Set
End Property
Public WriteOnly Property SetFootHeight() As Integer
Set(ByVal Value As Integer)
FootHeight = Value
End Set
End Property
Public WriteOnly Property SetSubFootHeight() As Integer
Set(ByVal Value As Integer)
SubFootHeight = Value
End Set
End Property
Public WriteOnly Property SetCellHeight() As Integer
Set(ByVal Value As Integer)
Y_unit = Value
End Set
End Property

Public Sub Print()
'Try
PrintDocument1 = New Printing.PrintDocument
AddHandler PrintDocument1.PrintPage, AddressOf Me.PrintDocument1_PrintPage

Dim PageSetup As PageSetupDialog
PageSetup = New PageSetupDialog
PageSetup.Document = PrintDocument1
PrintDocument1.DefaultPageSettings = PageSetup.PageSettings
If PageSetup.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
Exit Sub
End If

Pleft = PrintDocument1.DefaultPageSettings.Margins.Left
Ptop = PrintDocument1.DefaultPageSettings.Margins.Top
Pright = PrintDocument1.DefaultPageSettings.Margins.Right
Pbottom = PrintDocument1.DefaultPageSettings.Margins.Bottom
Pwidth = PrintDocument1.DefaultPageSettings.Bounds.Width
Pheigh = PrintDocument1.DefaultPageSettings.Bounds.Height

'将当前页分成基本的单元
X_unit = (Pwidth - Pleft - Pright) / DataTable1.Columns.Count - 1
PrintRecordNumber = (Pheigh - Ptop - Pbottom - HeadHeight - SubHeadHeight - FootHeight - SubFootHeight - Y_unit) / Y_unit

If DataTable1.Rows.Count > PrintRecordNumber Then
If DataTable1.Rows.Count Mod PrintRecordNumber = 0 Then
Totalpage = DataTable1.Rows.Count / PrintRecordNumber
Else
Totalpage = DataTable1.Rows.Count / PrintRecordNumber + 1
End If
Else
Totalpage = 1
End If

PrintDocument1.DocumentName = Totalpage.ToString
Dim PrintPriview As PrintPreviewDialog
PrintPriview = New PrintPreviewDialog
PrintPriview.Document = PrintDocument1
PrintPriview.WindowState = FormWindowState.Maximized
PrintPriview.ShowDialog()
'Catch ex As Exception
' MsgBox("打印错误,请检查打印设置!", 16, "错误")
'End Try
End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)

'Dim row_count As Integer '当前要打印的行

PrintRecordLeave = DataTable1.Rows.Count - PrintRecordComplete '还有多少条记录没有打印
If PrintRecordLeave > 0 Then
If PrintRecordLeave Mod PrintRecordNumber = 0 Then
PageNumber = PrintRecordLeave / PrintRecordNumber
Else
PageNumber = PrintRecordLeave / PrintRecordNumber + 1
End If
Else
PageNumber = 0
End If

'正在打印的页数
PrintingPageNumber = 0 '因为每打印一个新页都要计算还有多少页没有打印所以以打印的页数初始为0
'计算,余下的记录条数是否还可以在一页打印,不满一页时为假
If DataTable1.Rows.Count - PrintingPageNumber * PrintRecordNumber >= PrintRecordNumber Then
PageRecordNumber = PrintRecordNumber
Else
PageRecordNumber = (DataTable1.Rows.Count - PrintingPageNumber * PrintRecordNumber) Mod PrintRecordNumber
End If

Dim fmt As New StringFormat
fmt.LineAlignment = StringAlignment.Center '上下对齐
fmt.FormatFlags = StringFormatFlags.LineLimit '自动换行

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

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

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

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

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

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

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

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

End Sub

''' <summary>
''' '''''''''''''''''''''以上为打印代码
''' </summary>
''' <remarks></remarks>
'''

Private sqlconn As String ="......." '''''''用的SQLServer自己写了
Private conn As New SqlClient.SqlConnection(sqlconn)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s = queryqll()
End Sub
Public Function queryqll() As String
conn = New SqlClient.SqlConnection(sqlconn)
Dim CommandText = "select * from tablename " ''''查询一个表吧

Dim myCommand As New SqlClient.SqlDataAdapter(CommandText, conn)

Dim ds As New DataSet()
myCommand.Fill(ds, "T1")

DataGrid1.DataSource = ds.Tables("T1")
DataGrid1.ReadOnly = True''''datagridview 让他只读

Return 1
End Function

Private Sub dayreport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim s = queryqll()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

'''''这是触发的打印按钮

DataTable1 = DataGrid1.DataSource
HeadText = "表头不能只有身体啊!"
Call Print()

'''''别忘了 拉个 printdocument 下来
End Sub

End Class

分享到:
评论

相关推荐

    DataGridView打印示例.zip

    这个“DataGridView打印示例.zip”压缩包提供了一个C#项目,演示了如何实现`DataGridView`的打印功能,包括绘制表头、数据、打印预览、打印配置以及分页等关键环节。这对于初学者或者对打印模块不熟悉的开发者来说,...

    免费DataGridView打印及.NET轻松打印控件5.0版

    本DataGridView打印控件和.NET轻松打印控件5.0版(含报表模板设计组件)2012年5月12日修改完成,完全免费,在.NET2.0及以上环境下都可以使用(VB、C#等都可以用),有帮助文档与使用实例。 与4.95版相比,控件5.0版...

    c# DataGridView打印程序!

    而“C# DataGridView打印程序!”则是针对这个控件的一个特定实现,它扩展了`DataGridView`的功能,使其能够进行打印和打印预览,这对于数据分析和报表展示来说是非常有用的。 首先,我们要理解`DataGridView`控件的...

    DataGridView打印控件4.8版

    通过以上介绍,我们可以看出DataGridView打印控件4.8版不仅提升了打印功能,还在性能、用户体验、数据绑定、无障碍支持等多个方面进行了强化,为开发者提供了更强大、更易用的工具来构建数据驱动的应用程序。

    功能强大的免费DataGridView打印控件

    1、超强大的DataGridView打印功能,不光可以以多种形式(普通打印、分栏打印、跨页打印、工资条打印)打印DataGridView表格,基本上能完全按DataGridView控件本身设置的格式如字体、字号、背景颜色、前景颜色、...

    DataGridView打印。打印预览解决方案-第一部分

    这个控件允许用户查看、编辑和操作数据,而“DataGridView打印”与“打印预览”则是开发者常需面对的功能需求。在本教程中,我们将深入探讨如何实现`DataGridView`的打印和打印预览功能,这将分为多个步骤进行讲解。...

    datagridview打印组件

    在很多情况下,我们可能需要将DataGridView中的数据打印出来,这就需要用到所谓的“datagridview打印组件”。这个组件主要的功能是将DataGridView控件中的内容转换为可打印的格式,以便用户可以物理地输出到纸上。 ...

    DataGridView打印精灵组件

    我设计的一套DataGridView打印组件,欢迎大家使用 这个DLL对于.net直接打印DataGridView的数据非常方便。不需要借助第三方报表组件。 有问题或需要咨询,欢迎联系我。QQ 157864607

    datagridview打印控件

    首先,标题提到的“datagridview打印控件”通常指的是为DataGridView设计的专门用于打印其内容的扩展功能。在.NET Framework中,标准的DataGridView控件并不直接支持打印功能,因此开发者通常需要自定义或使用第三方...

    c# 实现datagridview完美打印功能

    在本文中,我们将深入探讨如何在C#中实现一个完善的DataGridView打印功能,包括选择打印字段、设置自定义标题以及仅打印选定的行。 首先,我们需要了解.NET Framework提供的Printing API,主要是System.Drawing....

    DataGridView打印精灵

    “DataGridView打印精灵”可根据各种不同设置方案,进行DataGridView的快速打印。(.NET 2.0)  使用这个DLL方法很简单,先需要添加对DLL的引用。再进行以下代码之添加: VB代码:  dim p as new JYWorks.打印精灵...

    datagridview打印类

    根据提供的信息,我们可以...通过以上分析可以看出,`datagridview打印类` 的实现不仅仅是关于打印功能本身,还涉及到数据加载、数据库访问、界面展示等多个方面。这些知识点对于理解和开发类似的应用程序都非常关键。

    C# DataGridView打印功能

    然而,有时我们需要将`DataGridView`中的数据打印出来,这就涉及到C#中的打印功能。本篇文章将深入探讨如何实现`DataGridView`的打印功能,包括解决列宽调整和分页技术,以及根据列宽自动调整行高的问题。 首先,...

    DataGridView打印控件4.7版

    本文将深入探讨`DataGridView`打印功能,特别是在“DataGridView打印控件4.7版”中的改进和特性。 首先,我们来了解`DataGridView`的基本概念。它是Windows Forms的一个核心控件,允许开发人员以网格形式展示数据,...

    dataGridView打印程序(可选择打印列和自定义打印标题)

    这个“dataGridView打印程序”是一个专门设计的工具,允许用户从dataGridView中选择特定列进行打印,并且可以自定义打印标题,提高了打印的灵活性和实用性。 在.NET框架中,dataGridView通常与System.Windows.Forms...

    VB.net DataGridView 打印控件

    首先,我们要了解在VB.NET中实现DataGridView打印的基本步骤。这通常涉及到以下几个环节: 1. **创建打印文档对象**:使用`PrintDocument`类创建一个打印文档实例,这是.NET Framework提供的打印基础类。通过这个...

    DataGridView打印控件.rar

    这个压缩包“DataGridView打印控件.rar”显然是为了帮助开发者学习如何在Windows应用程序中实现DataGridView控件的打印功能。以下是一些关于DataGridView打印功能的关键知识点: 1. **DataGridView控件**:它是...

    DataGridView打印专家组件及演示代码DataGridView打印专家组件及演示代码

    这就是“DataGridView打印专家组件”的作用所在。 这个组件专门设计用来解决`DataGridView`控件的打印问题,提供了一种高效、灵活的方式,将`DataGridView`中的数据转换为可打印的格式。通过使用这个组件,开发者...

    GirdView 和DataGridView打印与导出类

    在.NET框架中,GridView和DataGridView是两种常用的控件,用于...以上就是关于GridView和DataGridView打印与导出类的相关知识,通过理解和应用这些方法,开发者可以轻松实现对表格数据的打印和导出功能,提升用户体验。

Global site tag (gtag.js) - Google Analytics