论文部分内容阅读
摘要:本文介绍在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
关键词: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