摘要:本文介紹在Visual Basic程序中,利用Visual Basic操作Microsoft Excel,通過一個(gè)可攜帶記錄集參數(shù)的過程,實(shí)現(xiàn)對(duì)結(jié)構(gòu)不同、標(biāo)題不同的各類數(shù)據(jù)表的打??;克服了Visual Basic報(bào)表設(shè)計(jì)功能的局限性,解決了在程序運(yùn)行中,動(dòng)態(tài)生成的各種結(jié)構(gòu)不同的記錄集對(duì)應(yīng)的數(shù)據(jù)表的打印輸出問題;統(tǒng)一并簡化了Visual Basic應(yīng)用程序?qū)Ω黝惒煌Y(jié)構(gòu)數(shù)據(jù)表的設(shè)計(jì)及打印的實(shí)現(xiàn)方法。
關(guān)鍵詞:Visual Basic;Excel;數(shù)據(jù)表;打印
中圖分類號(hào):TP311文獻(xiàn)標(biāo)識(shí)碼:A文章編號(hào):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系統(tǒng)中報(bào)表功能的局現(xiàn)限
盡管Visual Basic系統(tǒng)中提供了設(shè)計(jì)打印報(bào)表功能,但是,需要對(duì)各種結(jié)構(gòu)不同的數(shù)據(jù)表分別去設(shè)計(jì),這是一項(xiàng)繁瑣的工作,并且要在程序設(shè)計(jì)階段確定數(shù)據(jù)表的結(jié)構(gòu)。然而,在信息管理系統(tǒng)中,常常需要程序運(yùn)行中,根據(jù)用戶輸入條件而生成各種結(jié)構(gòu)不同的記錄集,對(duì)于這種程序運(yùn)行中生成的記錄集,在程序設(shè)計(jì)時(shí)往往不能確定表的結(jié)構(gòu),要打印輸出這種動(dòng)態(tài)表,Visual Basic中的報(bào)表設(shè)計(jì)功能是無能為力的。
2 Visual Basic數(shù)據(jù)表通用打印程序
本人在信息管理系統(tǒng)開發(fā)應(yīng)用中,利用VB操作Excel,設(shè)計(jì)一個(gè)帶參數(shù)的過程,實(shí)現(xiàn)對(duì)各種結(jié)構(gòu)不同的數(shù)據(jù)表的打印輸出,使得對(duì)各類不同結(jié)構(gòu)數(shù)據(jù)表的設(shè)計(jì)及打印統(tǒng)一并簡化為對(duì)通用打印過程的一次調(diào)用,大大提高了VB程序設(shè)計(jì)中報(bào)表設(shè)計(jì)和打印的效率。
2.1 通用報(bào)表打印過程的參數(shù)
在標(biāo)準(zhǔn)模塊中聲明Public報(bào)表打印過程,過程的頭如下面語句所示:
Public Sub TablePrint(rs As ADODB.Recordset, Title As String)
其中TablePrint為過程名,參數(shù)rs是ADO記錄集對(duì)象,參數(shù)Title是字符串變量,這兩個(gè)參數(shù)分別將要打印報(bào)表對(duì)應(yīng)的記錄集和報(bào)表標(biāo)題帶入到打印過程,在打印過程中從這兩個(gè)參變量中獲取并輸出要打印報(bào)表的標(biāo)題、結(jié)構(gòu)及數(shù)據(jù)。
2.2 在Visual Basic中引用和創(chuàng)建外部Excel對(duì)象
在VB中操作Excel,首先要添加引用“Microsof Excel Object Library”。在設(shè)置引用Excel對(duì)象之后,在過程中聲明Excel應(yīng)用程序?qū)ο?、工作簿?duì)象并創(chuàng)建相關(guān)對(duì)象實(shí)例。下面語句依次聲明了Excel應(yīng)用程序?qū)ο髕lapp、工作簿對(duì)象xlbook。
Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
下面語句依次創(chuàng)建Excel應(yīng)用程序?qū)ο髮?shí)例、新工作簿文件實(shí)例及選擇工作表sheet1。
Set xlapp = CreateObject(\"excel.application\")
Set xlbook = xlapp.Workbooks.Add
xlapp.Sheets(1).Select
2.3 由記錄集的字段名建立表的列標(biāo)題
將記錄集輸出為數(shù)據(jù)表,首先要建立數(shù)據(jù)表的列標(biāo)題,本程序通過一個(gè)循環(huán)語句將記錄集的字段名即表的列標(biāo)題依次寫入當(dāng)前選定的工作表Sheet1的第1行。
For i = 0 Tors.FieldsCount-1
xlapp.Cells(1, i+1) = rs.Fields(i).Name
Next i
2.4 將記錄集中的數(shù)據(jù)寫入工作表
下面語句通過一個(gè)嵌套的循環(huán)將記錄集中的記錄數(shù)據(jù)依次寫入工作表。
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 記錄工作表的數(shù)據(jù)區(qū)域
為了使程序能自動(dòng)將數(shù)據(jù)表完整打印,程序必須記錄Excel工作表中的數(shù)據(jù)區(qū)域,下面語句分別獲取表格數(shù)據(jù)區(qū)域的最右列列號(hào)及右下角單元格地址,存入變量ec及ec1中。
ec = Chr(65 + rs.Fields.Count -1)
ecl = ec rs.RecordCount + 1
2.6 設(shè)置數(shù)據(jù)表的列寬及文字格式
在打印數(shù)據(jù)表之前,需要設(shè)置工作表的格式。
Excel工作表中的“Columns”對(duì)象的“AutoFit”方法,可實(shí)現(xiàn)工作表自動(dòng)調(diào)整列寬為最適合的列寬。
xlapp.Worksheets(1).Columns(\"A:\" ec).AutoFit
在選定工作表的數(shù)據(jù)區(qū)后,利用Excel工作表的相關(guān)屬性設(shè)置選定區(qū)域的字體、字號(hào)及文字對(duì)齊方式。
xlapp.Range(\"a1\", ecl).Select
With xlapp.Selection
.Font.Name = \"宋體\"
.Font.Size = 10
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
2.7 設(shè)置數(shù)據(jù)表的打印格式及打印輸出
在打印數(shù)據(jù)表之前,打印格式的設(shè)置是必不可少的。用With xlapp.ActiveSheet.PageSetup……End With括起來的下面一組語句實(shí)現(xiàn)了對(duì)數(shù)據(jù)表的打印設(shè)置。
設(shè)置打印區(qū)域、頁眉中打印數(shù)據(jù)表標(biāo)題和打印日期、頁腳中打印頁碼和頁數(shù)
.PrintArea = \"a1:\" ecl
.CenterHeader = Title \" (打印日期:\"\"Times New Roman,常規(guī)\"\"D\"\"宋體,常規(guī)\"\")\"
.CenterFooter = \"第 P 頁,共 N 頁\"
設(shè)置打印數(shù)據(jù)表的上、下、左、右、頁邊距及頁頭、頁腳邊距
.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)
設(shè)置打印表格線、設(shè)置每頁都要打印的列標(biāo)題、設(shè)置表的打印縮放比例。
.PrintGridlines = True
.PrintTitleRows = \"$1:$1\"
.Zoom = 95
下面語句實(shí)現(xiàn)將工作表打印,并且首先彈出打印預(yù)覽頁面,由用護(hù)選擇時(shí)否打印。
xlapp.ActiveWindow.SelectedSheets.PrintOut Preview:=True
2.8 完整的打印過程源代碼
Public Sub TablePrint(rs As ADODB.Recordset, Title As String)
'定義Excel應(yīng)用程序?qū)ο髕lapp、工作簿對(duì)象xlbook
Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
Set xlapp = CreateObject(\"excel.application\") '創(chuàng)建Excel應(yīng)用程序?qū)ο髮?shí)例
Set xlbook = xlapp.Workbooks.Add '創(chuàng)建新工作簿文件
xlapp.Sheets(1).Select '選擇工作表sheet1
xlapp.Visible = 1 '設(shè)置電子表格的可見性為假(調(diào)試時(shí)可設(shè)置為真,以便于觀察)
'將記錄集的字段(表的列標(biāo)題)寫入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
'獲取表格有效區(qū)域——即有數(shù)據(jù)的區(qū)域
ec = Chr(65 + rs.Fields.Count - 1)'最右邊列號(hào)
ecl = ec rs.RecordCount + 1 '最右下角單元格地址
'自動(dòng)調(diào)整表格列寬
xlapp.Worksheets(1).Columns(\"A:\" ec).AutoFit
'設(shè)置表格字體,字號(hào),單元格文字對(duì)齊
xlapp.Range(\"a1\", ecl).Select
With xlapp.Selection
.Font.Name = \"宋體\"
.Font.Size = 10
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
'打印設(shè)置
With xlapp.ActiveSheet.PageSetup
'設(shè)置頁眉:顯進(jìn)標(biāo)題和打印日期
.CenterHeader = Title \"(打印日期:\"\"Times New Roman,常規(guī)\"\"D\"\"宋體,常規(guī)\"\")\"
'設(shè)置頁腳
.CenterFooter = \"第 P 頁,共 N 頁\"
'設(shè)置上、下、左、右、頁邊距及頁頭、頁腳邊距
.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 '設(shè)置打印表的行列號(hào)
.PrintArea = \"a1:\" ecl'設(shè)置打印區(qū)域
.PrintGridlines = True '設(shè)置打印表格線
.PrintTitleRows = \"$1:$1\" '設(shè)置行標(biāo)題
.PrintTitleColumns = \"$A:$B\" '設(shè)置頁標(biāo)題
.CenterHorizontally = True '設(shè)置表格打印水平距中
.Zoom = 95'設(shè)置表格打印的縮放比例(也可做為參數(shù)代入)
End With
'打印預(yù)覽并打印
xlapp.ActiveWindow.SelectedSheets.PrintOut Preview:=True
End Sub
3 結(jié)束語
在Visual Basic應(yīng)用程序中,調(diào)用該過程,就可實(shí)現(xiàn)Visual Basic操作Excel程序在后臺(tái)設(shè)計(jì)及打印數(shù)據(jù)表,用戶看不到具體過程,只看到漂亮的報(bào)表輕易地被打印出來。
這種方法可以充分發(fā)揮MS Excel的報(bào)表設(shè)計(jì)打印功能,簡化了Visual Basic應(yīng)用程序中的報(bào)表設(shè)計(jì)工作,擺脫了Visual Basic中設(shè)計(jì)打印數(shù)據(jù)報(bào)表的煩腦。
參考文獻(xiàn):
[1] 劉文濤. Visual Basic+Access數(shù)據(jù)庫開發(fā)與實(shí)例[M]. 清華大學(xué)出版社,2006-07.
[2] 劉韜. Visual Basic 實(shí)效編程百例[M]. 人民郵電出版社,2004-08.
[3] 吳剛. 實(shí)現(xiàn)VB與EXCEL的無縫連接[M]. http://www.yesky.com/20030217/1652372.shtml,2003-02.