用printer物件列印表格 (轉)

worldblog發表於2007-12-04
用printer物件列印表格 (轉)[@more@]

  用printer列印表格

  用mlexgrid顯示的表格,要將它列印出來,最簡單的方法是用printform方法,然而這隻適合於資料正好能被螢幕顯示的,即資料量少的,而且這種列印效果很差。而用printer物件進行列印,雖然麻煩點,但效果卻是相當不錯的,你可以自定義列印格式,列印頁數,表格的粗細,字型大小等。實際上用printer物件進行列印程式設計是比較簡單的。

下面我就用一例項來說明:

列印的內容是一張資料表,這裡就只有兩列資料,包括標題,副標題。(用A4紙列印)

假設資料處在C_DataArray(),和R_DataArray()中C_Name與R_Name分別為兩資料項的欄位名

Public Sub Printtable()
'初始化

Dim printer1 as Printer

Dim pageheader
Dim pagefooter
Dim pageleft
Dim pageright
Dim usewidth
Dim useheight
Dim i, j, k As Integer
Dim 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)) / 2

printer1.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


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-987951/,如需轉載,請註明出處,否則將追究法律責任。

相關文章