`
isiqi
  • 浏览: 16884701 次
  • 性别: 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

分享到:
评论

相关推荐

    c#打印datagridview数据

    通过上述解析,可以看出该打印解决方案为C#开发者提供了一个灵活而强大的工具,用于处理复杂的数据打印需求。不仅可以轻松打印DataGridView中的数据,还能根据实际情况调整打印格式,从而满足各种业务场景的需求。

    C# dataGridView打印控件,可以打印dataGridView中的数据,同时可以把dataGridView数据导出到EXCEL中(源码)

    3. 创建Excel工作簿和工作表,使用循环结构将dataGridView数据写入Excel。 4. 调整Excel单元格样式,如字体、颜色、对齐方式等。 5. 保存Excel文件。 这两个功能在业务应用中非常实用,使得用户能够方便地打印报表...

    C#里面DataGridView控件数据打印

    本文将深入探讨如何在C#中利用DataGridView控件进行数据打印,包括如何处理包含条形码的打印需求。 首先,我们要了解DataGridView控件的基本用法。在C#中,通过Visual Studio的工具箱,可以将DataGridView拖放到...

    datagridview中的数据直接进行打印预览

    下面,我们将深入探讨如何实现`DataGridView`中的数据直接进行打印预览。 首先,我们需要了解`DataGridView`控件的基本操作。`DataGridView`是.NET Framework提供的一种用于显示表格数据的控件,它可以绑定到各种...

    DataGridView中的数据打印.rar

    DataGridView数据中的数据打印 DataGridView数据中的数据打印 DataGridView数据中的数据打印 DataGridView数据中的数据打印 DataGridView数据中的数据打印 DataGridView数据中的数据打印

    c# 实现datagridview完美打印功能

    然而,当用户需要将这些数据打印出来时,通常需要实现特定的打印功能。在本文中,我们将深入探讨如何在C#中实现一个完善的DataGridView打印功能,包括选择打印字段、设置自定义标题以及仅打印选定的行。 首先,我们...

    vb.net如何打印dataGridview中的数据

    以上就是使用VB.NET打印DataGridView数据的基本步骤。在实际应用中,可能还需要考虑更多的细节,比如页面设置、自定义样式、多列布局等问题。如果需要进一步定制,可以参考提供的链接...

    C# Winform Datagridview 分页和打印功能.pdf

    打印功能则是将DataGridView控件中的数据输出到打印机进行打印。 首先,关于DataGridView分页功能的实现,常用方法是通过自定义分页逻辑来处理。开发者可以通过引入一个分页控件来帮助用户选择页码,或手动控制...

    VB2005.NET打印DataGridview中的数据

    1. **创建打印类**:定义一个专门用于打印DataGridview数据的类。 2. **调用打印类**:在程序中实例化打印类,并传入需要打印的DataGridview对象及打印标题。 #### 创建打印类 首先,我们需要在Visual Studio中...

    C# 打印DataGridView的实现(完整实例)

    在实际项目中,有时我们需要将`DataGridView`中的数据打印出来,以便于用户查看或存档。本实例将详细讲解如何实现C#中打印`DataGridView`的功能,包括设置不同的打印样式、支持多页打印以及保证打印效果美观、可靠。...

    c# winform 打印DataGridView日期数据格式,格式化日期格式,可多页并可选择列打印

    在C# WinForm开发中,有时我们需要将DataGridView中的数据打印出来,这涉及到用户界面与打印功能的集成。本文将深入探讨如何实现一个功能完备的打印功能,包括日期数据的格式化,多页打印以及选择列打印。我们将参考...

    Winform控件dataGridView的打印与分页

    然而,对于一些应用场景而言,将 DataGridView 中的数据打印或进行分页处理是非常必要的。本文将详细介绍如何实现 DataGridView 控件的打印及分页功能。 #### 二、打印功能实现 ##### 2.1 打印组件介绍 为了实现 ...

    .net打印datagridview中的数据

    当你需要将`DataGridView`中的数据显示在纸质媒介上时,就需要涉及到打印功能。本文将深入探讨如何在.NET应用程序中实现打印`DataGridView`中的数据。 首先,了解基本的打印概念是必要的。在.NET中,`System....

    C#打印 DataGridView 控件

    代码简明扼要 我连DataGridView数据绑定都一起写了 想要三层实现的童鞋可联系俺) 也有说明 希望你会喜欢 "&gt;近段时间看到很多在路上的朋友都在找这个 我也偶尔在别的地方下载来看了下别人写的东西 感觉不是这缺就是...

    C#中datagridview实现打印功能

    然而,有时候我们需要将这些数据显示在纸质上,这就需要实现`DataGridView`的打印功能。在Visual Studio中,我们可以借助.NET Framework提供的类和方法来完成这一任务。下面将详细介绍如何在C#中利用`DataGridView`...

    C#打印DataGridView和打印窗体

    在C#编程中,打印功能是一项重要的任务,尤其是在开发企业级应用时,用户往往需要将数据或界面内容输出到纸质媒介上。本教程主要聚焦于如何利用C#来实现`DataGridView`控件的内容打印以及整个窗体的打印。首先,我们...

    DataGridView打印示例.zip

    总的来说,这个示例项目将引导你完成一个完整的`DataGridView`打印流程,从数据的准备、页面布局设计到用户交互,是理解和实践C#中`DataGridView`打印功能的宝贵资料。通过深入研究并运行这个示例,你可以更好地掌握...

    c# DataGridView打印程序!

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

    dataGridView实现数据打印浏览(未封装的源程序)

    在这个主题中,"dataGridView实现数据打印浏览(未封装的源程序)",我们将深入探讨如何使用C#或VB.NET通过dataGridView来实现数据的打印和预览功能,而这里的"未封装的源程序"意味着提供的代码可能没有进行高级的抽象...

Global site tag (gtag.js) - Google Analytics