报表打印的方法有很多种,比如说先导出
office
软件中然后进行排版布局,这样和方便,可是需要导入组件,而且必须安装
office
。下面我们看一种很实在的方法。提供全部源代码。以供朋友们学习交流。
@H_301_20@
这种方法也很简单,是使用
GDI+
把数据写到
PrintDoucument
中的
@H_301_20@
第一步
先引入命名空间
@H_301_20@
Imports
System
@H_301_20@
Imports
System.Drawing
@H_301_20@
Imports
System.Drawing.Text
@H_301_20@
第二步
编写
PrintDoucument
的
Printpage
事件
@H_301_20@
Private
Sub PrintDocument1_PrintPage(ByVal sender As System.Object,ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
@H_301_20@
下面
我们将数据表
mytab
中的数据写进
PrintDoucument
@H_301_20@
先定义画笔
@H_301_20@
Dim arialfont As FontFamily = New FontFamily("arial")
@H_301_20@
Dim font As Font = New Font(arialfont,10,FontStyle.Regular)
@H_301_20@
然后定义页边距
@H_301_20@
Dim rleft As Single = 10
@H_301_20@
Dim rtop As Single = e.MarginBounds.Top - 20
@H_301_20@
获取每一行的高度
@H_301_20@
Dim lineheight As Single = font.GetHeight(e.Graphics)
@H_301_20@
获取
Mytab
的行数和列数
@H_301_20@
Dim col As Integer = Mytab.Columns.Count
@H_301_20@
Dim row As Integer = Mytab.Rows.Count
@H_301_20@
获取
Mytab
的字段名称
@H_301_20@
Dim headline As String = ""
@H_301_20@
For i As Integer = 1 To col - 1
@H_301_20@
Dim coltext As String = Mytab.Columns(i).ColumnName.ToString
@H_301_20@
headline = headline & coltext & Space(1)
@H_301_20@
Next
@H_301_20@
获取要打印的数据
@H_301_20@
Dim listviewrows As String()
@H_301_20@
ReDim listviewrows(Me.ListView1.Items.Count + 1)
@H_301_20@
For i As Integer = 0 To row - 1
@H_301_20@
Dim lentext As String = ""
@H_301_20@
For j As Integer = 1 To col - 1
@H_301_20@
Dim maxlen As Integer = liewidth(j - 1)
@H_301_20@
Dim coltext As String
@H_301_20@
coltext = Mytab.Rows(i).item(j).ToString
@H_301_20@
lentext = lentext & coltext & Space(3)
@H_301_20@
Next
@H_301_20@
listviewrows(i) = lentext
@H_301_20@
Next
@H_301_20@
接下来就是真正的写入数据
@H_301_20@
Do
@H_301_20@
e.Graphics.DrawString(listviewrows(counts),font,Brushes.Black,rleft,rtop)
@H_301_20@
counts = counts + 1
@H_301_20@
rtop = rtop + lineheight
@H_301_20@
Loop Until rtop > e.MarginBounds.Bottom Or counts >= Mytab.Rows.Count - 1
@H_301_20@