Vb.net 如何实现报表打印

前端之家收集整理的这篇文章主要介绍了Vb.net 如何实现报表打印前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
报表打印的方法有很多种,比如说先导出 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@
If counts < Mytab.Rows.Count - 1 Then @H_301_20@
e.HasMorePages = True @H_301_20@
Else @H_301_20@
e.HasMorePages = False @H_301_20@
End If @H_301_20@
END Sub @H_301_20@
写好以后,就可以打印了 @H_301_20@
PrintDoucument.print() @H_301_20@
到此就可以把数据表 Mytab 中的所有数据打印出来了 @H_301_20@ 原文链接:https://www.f2er.com/vb/260763.html

猜你在找的VB相关文章