VB程序设计――数据表通用打印程序

时间:2022-09-23 05:02:04

VB程序设计――数据表通用打印程序

摘要:本文介绍在Visual Basic程序中,利用Visual Basic操作Microsoft Excel,通过一个可携带记录集参数的过程,实现对结构不同、标题不同的各类数据表的打印;克服了Visual Basic报表设计功能的局限性,解决了在程序运行中,动态生成的各种结构不同的记录集对应的数据表的打印输出问题;统一并简化了Visual Basic应用程序对各类不同结构数据表的设计及打印的实现方法。

关键词:Visual Basic;Excel;数据表;打印

中图分类号:TP311文献标识码:A文章编号:1009-3044(2008)09-11669-04

The VB Program Design: Universal Procedure to Print Report

ZHENG Xiao-hong

(From the Computer Science Department of Beijing Dongcheng Audlt College, Beijing 100020, China)

Abstract: This article will introduce, using the Visual Basic operation Microsost Excel in a application writen Visual Basic, how to print the dissimilar structure and diferen title reports with a procedure that takes a recordset parameter and a string type parameter. The procedure overcame the limit of function of the Visual Basic report design; solved the problem to print the different structure recordsets which were produce in a routine; unified and simplified the methos to design and print reports to each kind of different structure in a application writen Visual Basic.

Key words: Visual Basic; Excel; Print Report

1 Visual Basic系统中报表功能的局现限

尽管Visual Basic系统中提供了设计打印报表功能,但是,需要对各种结构不同的数据表分别去设计,这是一项繁琐的工作,并且要在程序设计阶段确定数据表的结构。然而,在信息管理系统中,常常需要程序运行中,根据用户输入条件而生成各种结构不同的记录集,对于这种程序运行中生成的记录集,在程序设计时往往不能确定表的结构,要打印输出这种动态表,Visual Basic中的报表设计功能是无能为力的。

2 Visual Basic数据表通用打印程序

本人在信息管理系统开发应用中,利用VB操作Excel,设计一个带参数的过程,实现对各种结构不同的数据表的打印输出,使得对各类不同结构数据表的设计及打印统一并简化为对通用打印过程的一次调用,大大提高了VB程序设计中报表设计和打印的效率。

2.1 通用报表打印过程的参数

在标准模块中声明Public报表打印过程,过程的头如下面语句所示:

Public Sub TablePrint(rs As ADODB.Recordset, Title As String)

其中TablePrint为过程名,参数rs是ADO记录集对象,参数Title是字符串变量,这两个参数分别将要打印报表对应的记录集和报表标题带入到打印过程,在打印过程中从这两个参变量中获取并输出要打印报表的标题、结构及数据。

2.2 在Visual Basic中引用和创建外部Excel对象

在VB中操作Excel,首先要添加引用“Microsof Excel Object Library”。在设置引用Excel对象之后,在过程中声明Excel应用程序对象、工作簿对象并创建相关对象实例。下面语句依次声明了Excel应用程序对象xlapp、工作簿对象xlbook。

Dim xlapp As Excel.Application

Dim xlbook As Excel.Workbook

下面语句依次创建Excel应用程序对象实例、新工作簿文件实例及选择工作表sheet1。

Set xlapp = CreateObject("excel.application")

Set xlbook = xlapp.Workbooks.Add

xlapp.Sheets(1).Select

2.3 由记录集的字段名建立表的列标题

将记录集输出为数据表,首先要建立数据表的列标题,本程序通过一个循环语句将记录集的字段名即表的列标题依次写入当前选定的工作表Sheet1的第1行。

For i = 0 Tors.FieldsCount-1

xlapp.Cells(1, i+1) = rs.Fields(i).Name

Next i

2.4 将记录集中的数据写入工作表

下面语句通过一个嵌套的循环将记录集中的记录数据依次写入工作表。

rs.MoveFirst

For j = 1 To rs.RecordCount

For i = 0 To rs.Fields.Count-1

xlapp.Cells(j+1, i+1) = rs.Fields(i).Value

Next i

rs.MoveNext

Next j

2.5 记录工作表的数据区域

为了使程序能自动将数据表完整打印,程序必须记录Excel工作表中的数据区域,下面语句分别获取表格数据区域的最右列列号及右下角单元格地址,存入变量ec及ec1中。

ec = Chr(65 + rs.Fields.Count -1)

ecl = ec & rs.RecordCount + 1

2.6 设置数据表的列宽及文字格式

在打印数据表之前,需要设置工作表的格式。

Excel工作表中的“Columns”对象的“AutoFit”方法,可实现工作表自动调整列宽为最适合的列宽。

xlapp.Worksheets(1).Columns("A:" & ec).AutoFit

在选定工作表的数据区后,利用Excel工作表的相关属性设置选定区域的字体、字号及文字对齐方式。

xlapp.Range("a1", ecl).Select

With xlapp.Selection

.Font.Name = "宋体"

.Font.Size = 10

.HorizontalAlignment = xlCenter

.VerticalAlignment = xlCenter

End With

2.7 设置数据表的打印格式及打印输出

在打印数据表之前,打印格式的设置是必不可少的。用With xlapp.ActiveSheet.PageSetup……End With括起来的下面一组语句实现了对数据表的打印设置。

设置打印区域、页眉中打印数据表标题和打印日期、页脚中打印页码和页数

.PrintArea = "a1:" & ecl

.CenterHeader = Title & " (打印日期:&""Times New Roman,常规""&D&""宋体,常规"")"

.CenterFooter = "第 &P 页,共 &N 页"

设置打印数据表的上、下、左、右、页边距及页头、页脚边距

.TopMargin = Application.InchesToPoints(1)

.BottomMargin = Application.InchesToPoints(1)

.LeftMargin = Application.InchesToPoints(0.5)

.RightMargin = Application.InchesToPoints(0.2)

.HeaderMargin = Application.InchesToPoints(0.5)

.FooterMargin = Application.InchesToPoints(0.5)

设置打印表格线、设置每页都要打印的列标题、设置表的打印缩放比例。

.PrintGridlines = True

.PrintTitleRows = "$1:$1"

.Zoom = 95

下面语句实现将工作表打印,并且首先弹出打印预览页面,由用护选择时否打印。

xlapp.ActiveWindow.SelectedSheets.PrintOut Preview:=True

2.8 完整的打印过程源代码

Public Sub TablePrint(rs As ADODB.Recordset, Title As String)

'定义Excel应用程序对象xlapp、工作簿对象xlbook

Dim xlapp As Excel.Application

Dim xlbook As Excel.Workbook

Set xlapp = CreateObject("excel.application") '创建Excel应用程序对象实例

Set xlbook = xlapp.Workbooks.Add '创建新工作簿文件

xlapp.Sheets(1).Select '选择工作表sheet1

xlapp.Visible = false '设置电子表格的可见性为假(调试时可设置为真,以便于观察)

'将记录集的字段(表的列标题)写入Sheet1表的第1行

For i = 0 To rs.Fields.Count - 1

xlapp.Cells(1, i + 1) = rs.Fields(i).Name

Next i

'将记录集中的记录写入Sheet1中

rs.MoveFirst

For j = 1 To rs.RecordCount

For i = 0 To rs.Fields.Count - 1

xlapp.Cells(j + 1, i + 1) = rs.Fields(i).Value

Next i

rs.MoveNext

Next j

'获取表格有效区域――即有数据的区域

ec = Chr(65 + rs.Fields.Count - 1)'最右边列号

ecl = ec & rs.RecordCount + 1 '最右下角单元格地址

'自动调整表格列宽

xlapp.Worksheets(1).Columns("A:" & ec).AutoFit

'设置表格字体,字号,单元格文字对齐

xlapp.Range("a1", ecl).Select

With xlapp.Selection

.Font.Name = "宋体"

.Font.Size = 10

.HorizontalAlignment = xlCenter

.VerticalAlignment = xlCenter

End With

'打印设置

With xlapp.ActiveSheet.PageSetup

'设置页眉:显进标题和打印日期

.CenterHeader = Title & "(打印日期:&""Times New Roman,常规""&D&""宋体,常规"")"

'设置页脚

.CenterFooter = "第 &P 页,共 &N 页"

'设置上、下、左、右、页边距及页头、页脚边距

.LeftMargin = Application.InchesToPoints(0.5)

.RightMargin = Application.InchesToPoints(0.2)

.TopMargin = Application.InchesToPoints(1)

.BottomMargin = Application.InchesToPoints(1)

.HeaderMargin = Application.InchesToPoints(0.5)

.FooterMargin = Application.InchesToPoints(0.5)

.PrintHeadings = False '设置打印表的行列号

.PrintArea = "a1:" & ecl'设置打印区域

.PrintGridlines = True '设置打印表格线

.PrintTitleRows = "$1:$1" '设置行标题

.PrintTitleColumns = "$A:$B" '设置页标题

.CenterHorizontally = True '设置表格打印水平距中

.Zoom = 95'设置表格打印的缩放比例(也可做为参数代入)

End With

'打印预览并打印

xlapp.ActiveWindow.SelectedSheets.PrintOut Preview:=True

End Sub

3 结束语

在Visual Basic应用程序中,调用该过程,就可实现Visual Basic操作Excel程序在后台设计及打印数据表,用户看不到具体过程,只看到漂亮的报表轻易地被打印出来。

这种方法可以充分发挥MS Excel的报表设计打印功能,简化了Visual Basic应用程序中的报表设计工作,摆脱了Visual Basic中设计打印数据报表的烦脑。

参考文献:

[1] 刘文涛. Visual Basic+Access数据库开发与实例[M]. 清华大学出版社,2006-07.

[2] 刘韬. Visual Basic 实效编程百例[M]. 人民邮电出版社,2004-08.

[3] 吴刚. 实现VB与EXCEL的无缝连接[M]. /20030217/1652372.shtml,2003-02.

上一篇:应用多Agent技术于高校教研资源管理的研究 下一篇:对IPv4/IPv6过渡技术的分析与研究