報(bào)表打印的方法有很多種,比如說先導(dǎo)出office 軟件中然后進(jìn)行排版布局,這樣和方便,可是需要導(dǎo)入組件,而且必須安裝office。下面我們看一種很實(shí)在的方法。提供全部源代碼。以供朋友們學(xué)習(xí)交流。 這種方法也很簡(jiǎn)單,是使用GDI+ 把數(shù)據(jù)寫到PrintDoucument中的 第一步 先引入命名空間 Imports System Imports System.Drawing Imports System.Drawing.Text 第二步 編寫PrintDoucument 的Printpage 事件 Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage 下面 我們將數(shù)據(jù)表 mytab 中的數(shù)據(jù)寫進(jìn)PrintDoucument 先定義畫筆 Dim arialfont As FontFamily = New FontFamily("arial") Dim font As Font = New Font(arialfont, 10, FontStyle.Regular) 然后定義頁(yè)邊距 Dim rleft As Single = 10 Dim rtop As Single = e.MarginBounds.Top - 20 獲取每一行的高度 Dim lineheight As Single = font.GetHeight(e.Graphics) 獲取Mytab的行數(shù)和列數(shù) Dim col As Integer = Mytab.Columns.Count Dim row As Integer = Mytab.Rows.Count 獲取Mytab的字段名稱 Dim headline As String = "" For i As Integer = 1 To col - 1 Dim coltext As String = Mytab.Columns(i).ColumnName.ToString headline = headline & coltext & Space(1) Next 獲取要打印的數(shù)據(jù) Dim listviewrows As String() ReDim listviewrows(Me.ListView1.Items.Count + 1) For i As Integer = 0 To row - 1 Dim lentext As String = "" For j As Integer = 1 To col - 1 Dim maxlen As Integer = liewidth(j - 1) Dim coltext As String coltext = Mytab.Rows(i).item(j).ToString lentext = lentext & coltext & Space(3) Next listviewrows(i) = lentext Next 接下來就是真正的寫入數(shù)據(jù) Do e.Graphics.DrawString(listviewrows(counts), font, Brushes.Black, rleft, rtop) counts = counts + 1 rtop = rtop + lineheight Loop Until rtop > e.MarginBounds.Bottom Or counts >= Mytab.Rows.Count - 1 If counts < Mytab.Rows.Count - 1 Then e.HasMorePages = True Else e.HasMorePages = False End If END Sub 寫好以后,就可以打印了 PrintDoucument.print() 到此就可以把數(shù)據(jù)表Mytab中的所有數(shù)據(jù)打印出來了 本文出自 “花花學(xué)編程” 博客,請(qǐng)務(wù)必保留此出處http://chenghua.blog.51cto.com/2306449/691908 |
|