发信人: cobe() 
整理人: fishy(2000-04-14 17:26:25), 站内信件
 | 
 
 
VB中Grid控件的打印方法 
                                                                               作者:武正伦 李俊海
 
                        rid控件是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 Int eger, 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_cli ck 
                      Dim fnt As Single 
                      Dim pp as integer 
                      Pp=0 '设置开始页码0 
                      Dim stry,strx,strx1,stry1,linw,page1,p As Integ er 
                      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设为"生成W ord表格", 写入下面代码: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设为"生成E XCEL表格" ,写入下面代码: 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 
                       
                    《计算机世界报》98年第34期9月7日 
  -- ■■■■■■■               请大家提问前务必浏览精华区
 ■ 专  V■     VB小世界
 ■ 用  B■     http://vbsworld.yeah.net
 ■ 章  版■     [email protected]
 ■ COBE■
 ■■■■■■■
  ※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.106.213.225]
  | 
 
 
 |