|
rid控件是VisualBasic最常见控件之一,从VB3.0到VB5.0都有该控件。也是VB爱好者最
喜爱的工具之一。用它可以以表格的形式显示、浏览数据,特别是数据库应用,直接绑定即可显
示浏览数据库信息。然而,美中不足的是Grid没有编辑和打印功能,列与列的位置不能相互交
换。笔者曾尝试着给Grid增添了这些功能,使之锦上添花,功能更强大。下面给出改进方法及源
程序,读者只需按步骤写下源程序即可使你的Grid具有打印功能。该程序笔者在HP5/100Window 95环境下用VB5.0调试通过。
给Grid控件增加打印方法有三种:1是直接打印控件的方法,2
是过printer来实现打印功能,3是通过调用MS-WORD及MS-EXCEl来实现打印。 首先,打开一个应用,在FORM1中增加DATA控件DATA1,把DATA1的CONNECT属性设为dBASEIII,
再把DATABASENAME属性设为D:\PJXM.DBF。然后再在FORM1中增加MSFLEXGRID空间GRID1,并把GRID1的DATASOURCE
属性设为DATA1。这样数据库PJXM.DBF的信息就会在GRID1中显示出来。
方法一:直 接打印窗体法,在FORM1中增加命令按钮(command),CAPTION属性设为直接打印,再写入下列 编码:
Subcommand_click Form1.printform Endsub
这样即可通过打印窗体FORM1的方法把GRID1的数据打印出来,遗憾的是只
能打印GRID1中显示的数据部分,显示不出来的则无法打印,而且这种打印方法很象屏幕硬拷 贝把其他控件也打印出来。也不能灵活的控制字体等。
方法二:通过PRINTER实现打印。 这种方法
1、加入打印命令按钮(command1)、函数(prnt1)即可实现打印功能,写入 下面代码,读者稍加改动可写成标准的函数或过程。
Functionprnt1(xAsInteger,
yAsInteger, fontAsSingle,txtAsString) printer.CurrentX=x printer.CurrentY=y printer.FontBold=False printer.FontSize=font printer.Printtxt EndFunction
Subcommand1_click DimfntAsSingle Dimppasinteger Pp=0设置开始页码0 Dimstry,strx,strx1,stry1,linw,page1,pAsInteger Statica(8)AsInteger定义打印的列数 ss$="内部结算存入款对帐单"定义表头 kan=0 Fori=0To8 a(i)=1500定义每列宽 kan=kan+a(i)计算表格总宽度 Next
page1=50定义每页行数 strx=200 strx1=200定义X方向起始位置 stry=1400 stry1=1400定义Y方向起始位置 linw
0定义行宽 fnt=8定义字体大小 printer.fontname="宋体"定义字体
dd=prnt1(4000,700,18,ss$)打印标题 printer.Line(strx-50,stry-30) -(strx+kan-10,stry-30) Forj=0Togridrow-1gridrow为所要打印的行数 grid1.row=j strx=strx1 printer.Line(strx-50,stry-30) -(strx+kan-10,stry-30) p=p+1 Fori=0To8 grid1.col=i dd=prnt1(strx,stry,fnt,grid1.text) strx=strx+a(i) Next
Ifp$#@62;page1Thennextpage p=0 strx=strx1 linelastline printer.Line(strx-50,stry+linw) -(strx+kan-10,stry+linw) stry=stry1 linecol Forn=0To8 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.NewPagenextpage dd=prnt1(4000,700,18,ss$)打印标题 strx=strx1 stry=stry1 printer.Line(strx-50,stry-30)- (strx+kan-10,stry-30)printfirstrow Else stry=stry+linw EndIf Next st=stry Ifp$#@60;page1Then在最后页剩余划空行 Foro=pTopage1+1 strx=strx1 printer.Line(strx-50,stry-30) -(strx+kan-10,stry-30) stry=stry+linw Next EndIf stry=stry1 strx=strx1 stry=stry1linecol Forn=0To8 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打印结束 Endsub
这种方法通过灵活的编程可以方便地调整字体、字型、线形、页面、纸张大小
等。可打印出比较满意的效果。如果你的计算机上装有MICROSOFTWORD和MICROEXCEL,最精彩
的用法还是把GRID的表格通过VB发送到MICROSOFTWORD及MICROEXCEL。生成MICROSOFTWORD
和MICROEXCEL表格。这样就可以充分利用MICROSOFTWORD和MICROEXCEL的打印、编辑功能 打印出更理想的效果。下面逐一介绍。
方法三:通过生成MICROSOFTWORD表格打印
1、在declaration中写入:
DimmswordAsObject
2、加入打印命令按 钮(command2),CAPTION设为"生成WORD表格",写入下面代码,
PrivateSub command2_Click()
screen.MousePointer=11 Setmsword=CreateObject("word.basic")
DimAppID,ReturnValue appID=Shell("d:\office97\office\WINWORD.EXE",1) RunMicrosoftWord.
msword.AppActivate"MicrosoftWord" msword.AppActivate"MicrosoftWord",1 full Screen.MousePointer=0 EndSub
2、写入以下过程full()
Subfull() DimiAsInteger,jAsInteger, colAsInteger,rowAsInteger DimcellcontentAsString Me.Hide cols=4表格的列数 row=gridrow打印表的行数 msword.filenewdefault msword.MsgBox"正在建立MS_WORD报表, 请稍候.......","",-1 msword.leftpara msword.screenupdating0 msword.tableinserttable,col,row,,,16,167 msword.startofdocument forj=0togridrow表格的行数 grid1.row=j Fori=1Tocols Gri1d.col=i IfIsNull(grid1.text)Then cellcontent$="" Else cellcontent$=grid1.text EndIf msword.Insertcellcontent$ msword.nextcell Nexti Nextj msword.tabledeleterow msword.startofdocument msword.tableselectrow msword.tableheadings1 msword.centerpara msword.startdocument msword.screenrefresh msword.screenupdating1 msword.MsgBox"结束","",-1 Me.Show
EndSub
方法四:通过发送到MICROSOFTEXCEL实现表格打印
1、加入打印命令按钮(command3),CAPTION设为"生成EXCEL表格",写入下面代 码
PrivateSubcommand3_Click() DimiAsInteger DimjAsInteger DimxlAppAsExcel.Application DimxlBookAsExcel.Workbook DimxlSheetAsExcel.Worksheet
SetxlApp=CreateObject("Excel.Application") xlApp.Visible=True SetxlBook=xlApp.Workbooks.Add OnErrorResumeNext SetxlBook=xlApp.Workbooks.AddOpen("d:\text2.xls") SetxlSheet=xlBook.Worksheets(1) xlSheet.Cells(6,1)="i" Fori=0Togridrow grid1.Row=i Forj=0To6 Grid1.Col=j
IfIsNull(Grid1.Text)=FalseThen xlSheet.Cells(i+5,j+1)=Grid1.Text EndIf Nextj Nexti ExitSub
[VB.NET程序]GSM短信模块库函数,可以用VB,VC,调用简单实用 [办公软件]PowerPoint做交互课件之弃用VBA [办公软件]VBA获取U盘、主板、CPU序列号和网卡MAC地址 [办公软件]VBA设置文件属性及加密源代码示例 [办公软件]VBA中初始化ADO连接的几种方法 [网络安全]“VB破坏者变种N”病毒摘要 [Web开发]ASP.NET上传文件到数据库VB版 [办公软件]在Excel中利用VBA实现多表单元格数据的读取与赋值… [办公软件]使用Vba读取已关闭的Excel工作薄数据到当前工作表… [办公软件]Excel编程基础之VBA文件操作详解
|