-
vb教程之VB中Grid控件的打印方法
Grid控件是Visual Basic(以下简称VB)最常见控件之一,用它可 以以表格的形式显示、浏览数据。特别是在数据库应用中,直接绑定 即可显示和浏览数据库信息。然而,美中不足的是Grid没有编辑和打 印功能,列与列的位置不能相互交换。笔者曾尝试着给Grid增添了这 些功能,使之功能更强大。下面给出改进方法及源程序,读者可参考使 用。该程序在HP5/100、Window 95环境下用VB5.0调试通过。
给Grid 控件增加打印方法有三种:一是直接打印控件,二是通过p rinter 来实现打印功能,三是通过调用MS-Word及MS-Excel 来实现打 印。
首先,打开一个应用,在FORM1中增加DATA控件DATA1,把DATA1的CO NNECT属性设为dBA SEⅢ,再把DATABASENAME属性设为D:\PJXM.DBF。 然后再在FORM1中增加MSFLEXGRID控件GRID1,并把GRID1的DATASOURCE 属性设为DATA1。这样数据库PJXM.DBF的信息就会在GRID1 中显示出 来。下面分析介绍各种打印方法。
方法一:直接打印窗体
在FORM1中增加命令按钮(command),CAPTION属性设为直接打印, 再写入下列代码:
Sub command_click
Form1.printform
End sub
这样即可通过打印窗体FORM1的方法把GRID1的数据打印出来。这 种方法的缺点是只能打印GRID1中显示的数据部分,显示不出来的则无 法打印,而且这种打印方法很像屏幕硬拷贝,而且不能灵活地控制字体 等。
方法二:通过PRINTER实现打印
1. 加入打印命令按钮(command1)和函数(prnt1),写入下面代码, 读者稍加改动可写成标准的函数或过程。Function prnt1 (x As Int eger, y As Integer, font As Sing le, txt As String)
printer.CurrentX = x
printer.CurrentY = y
printer.FontBold = False
printer.FontSize = font
printer.Print txt End Function Sub command1_click
Dim fnt As Single
Dim pp as integer
Pp=0 '设置开始页码0
Dim stry,strx,strx1,stry1,linw,page1,p As Integer
Static a(8) As Integer '定义打印的列数
ss$ = "内部结算存入款对帐单" '定义表头
kan = 0
For i = 0 To 8
a(i) = 1500' 定义每列宽
kan = kan + a(i) '计算表格总宽度
Next
page1 = 50 ’定义每页行数
strx = 200
strx1 = 200 ’定义X方向起始位置
stry = 1400
stry1 = 1400 ’定义Y方向起始位置
linw = 240 ’定义行宽
fnt = 8 ’定义字体大小
printer.fontname = "宋体" ’定义字体
dd = prnt1(4000, 700, 18, ss$) ’打印标题
printer.Line (strx - 50, stry - 30)-(strx +
kan - 10, stry - 30)
For j = 0 To gridrow - 1
’gridrow为所要打印的行数
grid1.row = j
strx = strx1
printer.Line (strx - 50, stry - 30)-(strx +
kan - 10, stry - 30)
p = p + 1
For i = 0 To 8
grid1.col = i
dd = prnt1(strx, stry, fnt, grid1.text)
strx = strx + a(i)
Next
If p > page1 Then ’next page
p = 0
strx = strx1
printer.Line (strx - 50, stry + linw)-(strx + kan - 10, stry + linw)
stry = stry1
For n = 0 To 8
printer.Line (strx - 30, stry - 30)-(strx
- 30, stry + (page1 + 2) * linw)
strx = strx + a(n)
Next
printer.Line (strx - 30, stry - 30)-(strx -
30, stry + (page1 + 2) * linw)
pp=pp+1
foot$="第 "+cstr(pp)+" 页"
dd = prnt1(strx - 30-1000, stry + (page1 +
2) * linw+100, 10, foot$) ’打印页脚码
printer.NewPage ’next page
dd = prnt1(4000, 700, 18, ss$) ’打印标题
strx = strx1
stry = stry1
printer.Line (strx - 50, stry - 30)-(strx +
kan - 10, stry - 30) ’打印第一行
Else
stry = stry + linw
End If Next
st = stry
If p < page1 Then ’在最后页剩余划空行
For o = p To page1 + 1
strx = strx1
printer.Line (strx - 50, stry - 30)-(strx +
kan - 10, stry - 30)
stry = stry + linw
Next
End If
stry = stry1
strx = strx1
stry = stry1’line col
For n = 0 To 8
printer.Line (strx - 30, stry - 30)-(strx -
30, stry + (page1 + 2) * linw)
strx = strx + a(n)
Next
printer.Line (strx - 30, stry - 30)-(strx -
30, stry + (page1 + 2) * linw)
pp=pp+1
foot$="第 "+cstr(pp)+" 页"
dd = prnt1(strx - 30-1000, stry + (page1 +
2) * linw+100, 10, foot$) ’打印页脚码
printer.EndDoc ’打印结束
End sub
这种方法通过灵活的编程可以方便地调整字体、字型、线形、页 面、纸张大小等,可打印出比较满意的效果。
如果计算机上装有Microsoft Word和Microsoft Excel,则可把GR ID的表格通过VB发送到这些应用程序中去,生成Microsoft Word和Mic rosoft Excel表格。这样可以充分利用它们的打印、编辑功能,打印 出更理想的效果。下面逐一介绍。
方法三:通过生成Word表格打印
1. 在declaration 中写入:
Dim msword As Object
2. 加入打印命令按钮(command2),CAPTION设为"生成Word表格", 写入下面代码:Pri vate Sub command2_Click()
screen.MousePointer = 11
Set msword = CreateObject("word.basic")
Dim AppID, ReturnValue
appID = Shell("d:\office97\office\WIN-
WORD.EXE", 1) ’运行Word
msword.AppActivate "Microsoft Word"
full
Screen.MousePointer = 0 End Sub
3. 写入以下过程full() Sub full()
Dim i As Integer, j As Integer, col As Integer,
row As Integer
Dim cellcontent As String
Me.Hide
cols = 4 ’表格的列数
row = gridrow ’打印表的行数
msword.filenewdefault
msword.MsgBox "正在建立MS_WORD报表,请稍候
……", "", -1
msword.leftpara
msword.screenupdating 0
msword.tableinserttable , col, row, , , 16, 167
msword.startofdocument
for j=0 to gridrow ’表格的行数
grid1.row=j
For i = 1 To cols
Gri1d.col=i
If IsNull(grid1.text) Then
cellcontent$ = ""
Else
cellcontent$ = grid1.text
End If
msword.Insert cellcontent$
msword.nextcell
Next i
Next j
msword.tabledeleterow
msword.startofdocument
msword.tableselectrow
msword.tableheadings 1
msword.centerpara
’msword.startdocument
msword.screenrefresh
msword.screenupdating 1
msword.MsgBox "结束", "", -1
Me.Show End Sub
方法四:通过发送到Excel实现表格打印
1. 加入打印命令按钮(command3),CAPTION设为"生成EXCEL表格" ,写入下面代码: Private Sub command3_Click()
Dim i As Integer
Dim j As Integer
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
xlSheet.Cells(6, 1) = "i"
For i = 0 To gridrow
grid1.Row = i
For j = 0 To 6
Grid1.Col = j
If IsNull(Grid1.Text) = False Then
xlSheet.Cells(i + 5, j + 1) = Grid1.Text
End If
Next j
Next i End Sub
给Grid 控件增加打印方法有三种:一是直接打印控件,二是通过p rinter 来实现打印功能,三是通过调用MS-Word及MS-Excel 来实现打 印。
首先,打开一个应用,在FORM1中增加DATA控件DATA1,把DATA1的CO NNECT属性设为dBA SEⅢ,再把DATABASENAME属性设为D:\PJXM.DBF。 然后再在FORM1中增加MSFLEXGRID控件GRID1,并把GRID1的DATASOURCE 属性设为DATA1。这样数据库PJXM.DBF的信息就会在GRID1 中显示出 来。下面分析介绍各种打印方法。
方法一:直接打印窗体
在FORM1中增加命令按钮(command),CAPTION属性设为直接打印, 再写入下列代码:
Sub command_click
Form1.printform
End sub
这样即可通过打印窗体FORM1的方法把GRID1的数据打印出来。这 种方法的缺点是只能打印GRID1中显示的数据部分,显示不出来的则无 法打印,而且这种打印方法很像屏幕硬拷贝,而且不能灵活地控制字体 等。
方法二:通过PRINTER实现打印
1. 加入打印命令按钮(command1)和函数(prnt1),写入下面代码, 读者稍加改动可写成标准的函数或过程。Function prnt1 (x As Int eger, y As Integer, font As Sing le, txt As String)
printer.CurrentX = x
printer.CurrentY = y
printer.FontBold = False
printer.FontSize = font
printer.Print txt End Function Sub command1_click
Dim fnt As Single
Dim pp as integer
Pp=0 '设置开始页码0
Dim stry,strx,strx1,stry1,linw,page1,p As Integer
Static a(8) As Integer '定义打印的列数
ss$ = "内部结算存入款对帐单" '定义表头
kan = 0
For i = 0 To 8
a(i) = 1500' 定义每列宽
kan = kan + a(i) '计算表格总宽度
Next
page1 = 50 ’定义每页行数
strx = 200
strx1 = 200 ’定义X方向起始位置
stry = 1400
stry1 = 1400 ’定义Y方向起始位置
linw = 240 ’定义行宽
fnt = 8 ’定义字体大小
printer.fontname = "宋体" ’定义字体
dd = prnt1(4000, 700, 18, ss$) ’打印标题
printer.Line (strx - 50, stry - 30)-(strx +
kan - 10, stry - 30)
For j = 0 To gridrow - 1
’gridrow为所要打印的行数
grid1.row = j
strx = strx1
printer.Line (strx - 50, stry - 30)-(strx +
kan - 10, stry - 30)
p = p + 1
For i = 0 To 8
grid1.col = i
dd = prnt1(strx, stry, fnt, grid1.text)
strx = strx + a(i)
Next
If p > page1 Then ’next page
p = 0
strx = strx1
printer.Line (strx - 50, stry + linw)-(strx + kan - 10, stry + linw)
stry = stry1
For n = 0 To 8
printer.Line (strx - 30, stry - 30)-(strx
- 30, stry + (page1 + 2) * linw)
strx = strx + a(n)
Next
printer.Line (strx - 30, stry - 30)-(strx -
30, stry + (page1 + 2) * linw)
pp=pp+1
foot$="第 "+cstr(pp)+" 页"
dd = prnt1(strx - 30-1000, stry + (page1 +
2) * linw+100, 10, foot$) ’打印页脚码
printer.NewPage ’next page
dd = prnt1(4000, 700, 18, ss$) ’打印标题
strx = strx1
stry = stry1
printer.Line (strx - 50, stry - 30)-(strx +
kan - 10, stry - 30) ’打印第一行
Else
stry = stry + linw
End If Next
st = stry
If p < page1 Then ’在最后页剩余划空行
For o = p To page1 + 1
strx = strx1
printer.Line (strx - 50, stry - 30)-(strx +
kan - 10, stry - 30)
stry = stry + linw
Next
End If
stry = stry1
strx = strx1
stry = stry1’line col
For n = 0 To 8
printer.Line (strx - 30, stry - 30)-(strx -
30, stry + (page1 + 2) * linw)
strx = strx + a(n)
Next
printer.Line (strx - 30, stry - 30)-(strx -
30, stry + (page1 + 2) * linw)
pp=pp+1
foot$="第 "+cstr(pp)+" 页"
dd = prnt1(strx - 30-1000, stry + (page1 +
2) * linw+100, 10, foot$) ’打印页脚码
printer.EndDoc ’打印结束
End sub
这种方法通过灵活的编程可以方便地调整字体、字型、线形、页 面、纸张大小等,可打印出比较满意的效果。
如果计算机上装有Microsoft Word和Microsoft Excel,则可把GR ID的表格通过VB发送到这些应用程序中去,生成Microsoft Word和Mic rosoft Excel表格。这样可以充分利用它们的打印、编辑功能,打印 出更理想的效果。下面逐一介绍。
方法三:通过生成Word表格打印
1. 在declaration 中写入:
Dim msword As Object
2. 加入打印命令按钮(command2),CAPTION设为"生成Word表格", 写入下面代码:Pri vate Sub command2_Click()
screen.MousePointer = 11
Set msword = CreateObject("word.basic")
Dim AppID, ReturnValue
appID = Shell("d:\office97\office\WIN-
WORD.EXE", 1) ’运行Word
msword.AppActivate "Microsoft Word"
full
Screen.MousePointer = 0 End Sub
3. 写入以下过程full() Sub full()
Dim i As Integer, j As Integer, col As Integer,
row As Integer
Dim cellcontent As String
Me.Hide
cols = 4 ’表格的列数
row = gridrow ’打印表的行数
msword.filenewdefault
msword.MsgBox "正在建立MS_WORD报表,请稍候
……", "", -1
msword.leftpara
msword.screenupdating 0
msword.tableinserttable , col, row, , , 16, 167
msword.startofdocument
for j=0 to gridrow ’表格的行数
grid1.row=j
For i = 1 To cols
Gri1d.col=i
If IsNull(grid1.text) Then
cellcontent$ = ""
Else
cellcontent$ = grid1.text
End If
msword.Insert cellcontent$
msword.nextcell
Next i
Next j
msword.tabledeleterow
msword.startofdocument
msword.tableselectrow
msword.tableheadings 1
msword.centerpara
’msword.startdocument
msword.screenrefresh
msword.screenupdating 1
msword.MsgBox "结束", "", -1
Me.Show End Sub
方法四:通过发送到Excel实现表格打印
1. 加入打印命令按钮(command3),CAPTION设为"生成EXCEL表格" ,写入下面代码: Private Sub command3_Click()
Dim i As Integer
Dim j As Integer
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
xlSheet.Cells(6, 1) = "i"
For i = 0 To gridrow
grid1.Row = i
For j = 0 To 6
Grid1.Col = j
If IsNull(Grid1.Text) = False Then
xlSheet.Cells(i + 5, j + 1) = Grid1.Text
End If
Next j
Next i End Sub
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比