用VB控制EXCEL生成报表 做为一种简捷、系统的 Windows应用程序开发工具,Visual Basic 5 具有强大的数据处理功能,提供了多种数据访问方法,可以方便地存取Microsoft SQL Server、Oracle、XBase等多种数据库,被广泛应用于建立各种信息管理系统。但是,VB缺乏足够的、符合中文习惯的数据表格输出功能,虽然使用Crystal Report控件及 Crystal Reports程序可以输出报表,但操作起来很麻烦,中文处理能力也不理想。Excel作为Micorsoft公司的表格处理软件在表格方面有着强大的功能,我们可用VB5编写直接控制Excel操作的程序,方法是用VB的OLE自动化技术获取Excel 97 的控制句柄,从而直接控制Excel 97的一系列操作。下面给出一个实例:首先建立一个窗体(FORM1)在窗体中加入一个DATA控件和一按钮,引用Microsoft Excel类型库:从"工程"菜单中选择"引用"栏;选择Microsoft Excel 8.0 Object Library;选择"确定"。在FORM的LOAD事件中加入: Data1.DatabaseName = 数据库名称 Data1.RecordSource = 表名 Data1.Refresh在按钮的CLICK事件中加入 Dim Irow, Icol As Integer Dim Irowcount, Icolcount As Integer Dim Fieldlen() "存字段长度值 Dim xlApp As Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet Set xlApp = CreateObject("Excel.Application") Set xlBook = xlApp.Workbooks.Add Set xlSheet = xlBook.Worksheets(1) With Data1.Recordset .MoveLast If .RecordCount < 1 Then MsgBox ("Error 没有记录!") Exit Sub End If Irowcount = .RecordCount "记录总数 Icolcount = .Fields.Count "字段总数 ReDim Fieldlen(Icolcount) .MoveFirst8 For Irow = 1 To Irowcount + 1 For Icol = 1 To Icolcount Select Case Irow Case 1 "在Excel中的第一行加标题 xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1).Name Case 2 "将数组FIELDLEN()存为第一条记录的字段长 If IsNull(.Fields(Icol - 1)) = True Then Fieldlen(Icol) = LenB(.Fields(Icol - 1).Name) "如果字段值为NULL,则将数组Filelen(Icol)的值设为标题名的宽度 Else Fieldlen(Icol) = LenB(.Fields(Icol - 1)) End If xlSheet.Columns(Icol).ColumnWidth = Fieldlen(Icol) "Excel列宽等于字段长 xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1) "向Excel的CellS中写入字段值 Case Else Fieldlen1 = LenB(.Fields(Icol - 1)) If Fieldlen(Icol) < Fieldlen1 Then xlSheet.Columns(Icol).ColumnWidth = Fieldlen1 "表格列宽等于较长字段长 Fieldlen(Icol) = Fieldlen1 "数组Fieldlen(Icol)中存放最大字段长度值 Else xlSheet.Columns(Icol).ColumnWidth = Fieldlen(Icol) End If xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1) End Select Next If Irow <> 1 Then If Not .EOF Then .MoveNext End If Next With xlSheet .Range(.Cells(1, 1), .Cells(1, Icol - 1)).Font.Name = "黑体" "设标题为黑体字 .Range(.Cells(1, 1), .Cells(1, Icol - 1)).Font.Bold = True "标题字体加粗 .Range(.Cells(1, 1), .Cells(Irow, Icol - 1)).Borders.LineStyle = xlContinuous "设表格边框样式 End With xlApp.Visible = True "显示表格 xlBook.Save "保存 Set xlApp = Nothing "交还控制给Excel End With本程序在中文Windows98、中文VB5下通过。
用printer对象打印表格 用msflexgrid控件显示的表格,要将它打印出来,最简单的方法是用printform方法,然而这只适合于数据正好能被屏幕显示的,即数据量少的,而且这种打印效果很差。而用printer对象进行打印编程,虽然麻烦点,但效果却是相当不错的,你可以自定义打印格式,打印页数,表格的粗细,字体大小等。实际上用printer对象进行打印编程是比较简单的。下面我就用一实例来说明:打印的内容是一张数据表,这里就只有两列数据,包括标题,副标题。(用A4纸打印)假设数据处在C_DataArray(),和R_DataArray()中C_Name与R_Name分别为两数据项的字段名Public Sub Printtable() '初始化Dim printer1 as PrinterDim pageheader Dim pagefooter Dim pageleft Dim pageright Dim usewidth Dim useheight Dim i, j, k As Integer Dim word As String Dim startx Dim starty Dim startyline ‘ 用来纪录打印竖线的起点Dim endyline ’ 用来纪录打印竖线的末点设置页面参数pageheader = 25 pagefooter = 25 pageleft = 20 pageright = 20 With printer1 .PaperSize = 9 .ScaleMode = 6 .FontBold = True .ScaleLeft = -20 .ScaleTop = -25 .ScaleWidth = 210 '设置为A4纸 .ScaleHeight = 297 usewidth = .ScaleWidth - 40 useheight = .ScaleHeight - 50 .CurrentX = 0 .CurrentY = 0 .DrawWidth = 5 End With '打印标题 With printer1 .FontSize = 20 .CurrentX = (usewidth - .TextWidth(DataTitle)) / 2 .CurrentY = pageheader + .ScaleTop End With printer1.Print DataTitle
'打印副标题 printer1.FontSize = 15 word = DataTitle2 printer1.CurrentX = usewidth - printer1.TextWidth(word) printer1.Print word
做为一种简捷、系统的 Windows应用程序开发工具,Visual Basic 5 具有强大的数据处理功能,提供了多种数据访问方法,可以方便地存取Microsoft SQL Server、Oracle、XBase等多种数据库,被广泛应用于建立各种信息管理系统。但是,VB缺乏足够的、符合中文习惯的数据表格输出功能,虽然使用Crystal Report控件及 Crystal Reports程序可以输出报表,但操作起来很麻烦,中文处理能力也不理想。Excel作为Micorsoft公司的表格处理软件在表格方面有着强大的功能,我们可用VB5编写直接控制Excel操作的程序,方法是用VB的OLE自动化技术获取Excel 97 的控制句柄,从而直接控制Excel 97的一系列操作。下面给出一个实例:首先建立一个窗体(FORM1)在窗体中加入一个DATA控件和一按钮,引用Microsoft Excel类型库:从"工程"菜单中选择"引用"栏;选择Microsoft Excel 8.0 Object Library;选择"确定"。在FORM的LOAD事件中加入:
Data1.DatabaseName = 数据库名称
Data1.RecordSource = 表名
Data1.Refresh在按钮的CLICK事件中加入
Dim Irow, Icol As Integer
Dim Irowcount, Icolcount As Integer
Dim Fieldlen() "存字段长度值
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1) With Data1.Recordset
.MoveLast If .RecordCount < 1 Then
MsgBox ("Error 没有记录!")
Exit Sub
End If Irowcount = .RecordCount "记录总数
Icolcount = .Fields.Count "字段总数 ReDim Fieldlen(Icolcount)
.MoveFirst8 For Irow = 1 To Irowcount + 1
For Icol = 1 To Icolcount
Select Case Irow
Case 1 "在Excel中的第一行加标题
xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1).Name
Case 2 "将数组FIELDLEN()存为第一条记录的字段长 If IsNull(.Fields(Icol - 1)) = True Then
Fieldlen(Icol) = LenB(.Fields(Icol - 1).Name)
"如果字段值为NULL,则将数组Filelen(Icol)的值设为标题名的宽度
Else
Fieldlen(Icol) = LenB(.Fields(Icol - 1))
End If xlSheet.Columns(Icol).ColumnWidth = Fieldlen(Icol)
"Excel列宽等于字段长
xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1)
"向Excel的CellS中写入字段值
Case Else
Fieldlen1 = LenB(.Fields(Icol - 1)) If Fieldlen(Icol) < Fieldlen1 Then
xlSheet.Columns(Icol).ColumnWidth = Fieldlen1
"表格列宽等于较长字段长
Fieldlen(Icol) = Fieldlen1
"数组Fieldlen(Icol)中存放最大字段长度值
Else
xlSheet.Columns(Icol).ColumnWidth = Fieldlen(Icol)
End If xlSheet.Cells(Irow, Icol).Value = .Fields(Icol - 1)
End Select
Next
If Irow <> 1 Then
If Not .EOF Then .MoveNext
End If
Next
With xlSheet
.Range(.Cells(1, 1), .Cells(1, Icol - 1)).Font.Name = "黑体"
"设标题为黑体字
.Range(.Cells(1, 1), .Cells(1, Icol - 1)).Font.Bold = True
"标题字体加粗
.Range(.Cells(1, 1), .Cells(Irow, Icol - 1)).Borders.LineStyle = xlContinuous
"设表格边框样式
End With
xlApp.Visible = True "显示表格
xlBook.Save "保存
Set xlApp = Nothing "交还控制给Excel
End With本程序在中文Windows98、中文VB5下通过。
'初始化Dim printer1 as PrinterDim pageheader
Dim pagefooter
Dim pageleft
Dim pageright
Dim usewidth
Dim useheight
Dim i, j, k As Integer
Dim word As String
Dim startx
Dim starty
Dim startyline ‘ 用来纪录打印竖线的起点Dim endyline ’ 用来纪录打印竖线的末点设置页面参数pageheader = 25
pagefooter = 25
pageleft = 20
pageright = 20
With printer1
.PaperSize = 9
.ScaleMode = 6
.FontBold = True
.ScaleLeft = -20
.ScaleTop = -25
.ScaleWidth = 210 '设置为A4纸
.ScaleHeight = 297
usewidth = .ScaleWidth - 40
useheight = .ScaleHeight - 50
.CurrentX = 0
.CurrentY = 0
.DrawWidth = 5
End With
'打印标题
With printer1
.FontSize = 20
.CurrentX = (usewidth - .TextWidth(DataTitle)) / 2
.CurrentY = pageheader + .ScaleTop
End With
printer1.Print DataTitle
'打印副标题
printer1.FontSize = 15
word = DataTitle2
printer1.CurrentX = usewidth - printer1.TextWidth(word)
printer1.Print word
'打印第一条线 Line方法不能用在with ....end with里printer1.CurrentX = pageleft + printer1.ScaleLeft
startyline = printer1.CurrentY
'线宽printer1.Line -((printer1.ScaleLeft + printer1.ScaleWidth - pageleft), printer1.CurrentY)
printer1.FontSize = 10
'printer1.Print vbLf
printer1.CurrentY = printer1.CurrentY + 1
'打印第一个字段名
starty = printer1.CurrentY
printer1.CurrentX = ((printer1.ScaleWidth - 40) / 2 - printer1.TextWidth(C_Name)) / 2printer1.Print C_Name
'打印第二个字段名
printer1.CurrentX = usewidth / 2 + ((usewidth / 2 - printer1.TextWidth(R_Name)) / 2)
printer1.CurrentY = starty
printer1.Print R_Name
printer1.CurrentY = printer1.CurrentY + 1
'打印数据和横线,rownum为数据行数For i = 1 To rownum
'判断是否该页已打满
If printer1.CurrentY >= useheight Then '打印横线
printer1.CurrentX = printer1.ScaleLeft + pageleft
printer1.Line -((printer1.ScaleLeft + printer1.ScaleWidth - pageleft), printer1.CurrentY)
printer1.CurrentY = printer1.CurrentY + 1
'打印三条竖线
endyline = printer1.CurrentY
printer1.Line (0, startyline)-(0, endyline)
printer1.Line (usewidth / 2, startyline)-(usewidth / 2, endyline)
printer1.Line (usewidth, startyline)-(usewidth, endyline)
'打印页号
With printer1
.CurrentX = (.ScaleWidth - .TextWidth(.Page)) / 2 - pageleft
.CurrentY = useheight + 3
End With
printer1.Print printer1.Page
printer1.NewPage
With printer1
.CurrentX = pageleft + .ScaleLeft
.CurrentY = pageheader + .ScaleTop
startyline = .CurrentY
End With
End If
'打印一行数据
printer1.CurrentX = ((printer1.ScaleWidth - 40) / 2 - printer1.TextWidth(C_DataArray(i))) / 2
starty = printer1.CurrentY
printer1.Print C_DataArray(i)
printer1.CurrentX = (printer1.ScaleWidth - 40) / 2 + ((printer1.ScaleWidth - 40) / 2 - printer1.TextWidth(R_DataArray(i) )) / 2
printer1.CurrentY = starty
printer1.Print R_DataArray(i)
printer1.CurrentY = printer1.CurrentY + 1
Next i
'打印最后一条横线
printer1.CurrentX = printer1.ScaleLeft + pageleft
printer1.Line -((printer1.ScaleLeft + printer1.ScaleWidth - pageleft), printer1.CurrentY)
endyline = printer1.CurrentY
'打印三条竖线
printer1.Line (0, startyline)-(0, endyline)
printer1.Line (usewidth / 2, startyline)-(usewidth / 2, endyline)
printer1.Line (usewidth, startyline)-(usewidth, endyline)
'打印页号
With printer1
.CurrentX = (.ScaleWidth - .TextWidth(.Page)) / 2 - pageleft
.CurrentY = useheight + 3
End With
printer1.Print printer1.Page
printer1.EndDoc
end sub