當前位置:編程學習大全網 - 編程語言 - 在窗體中實現打印預覽功能,實現以下非規範表格和數據 的打印

在窗體中實現打印預覽功能,實現以下非規範表格和數據 的打印

VB下數據報表打印輸出方法有多種,壹是通過自己的設計和編程完成報表打印輸出。二是通過MS OFFICE實現報表打印輸出。三是應用數據報表設計器的報表打印輸出。本人經常使用第壹種方法,它具有根據需要靈活應用的特點,但需編寫程序。下附代碼,第壹段代碼定義基本變量,第二段代碼定義打印函數,第三段代碼是具體打印代碼,含數據轉換。第四段代碼是窗體加載代碼,具體連接數據庫和打印代碼根據妳所涉及的數據要求進行修改。預覽代碼在如下網址:

/question/4165080.html

報表打印代碼:

Option Explicit

Private n As Integer

Private m As Integer

Private zh_dm(10) As Double

Private dy_dm(3000, 10) As Variant

Private zsl As Integer

Private y As Integer

Private x As Integer

Private i As Integer

Private k As Integer

Private p As Integer

Private pa As Integer

Private j As Integer

Private txt As String

Private fnt As Integer

Private dd As Variant

Public Function prnt(x As Variant, y As Variant, fnt As Variant, txt0 As Variant)

Printer.CurrentX = x

Printer.CurrentY = y

Printer.FontSize = fnt

Printer.Print txt0

End Function

Private Sub cmdPrint_Click()

Adodc1.Recordset.MoveFirst

For i = 0 To zsl - 1

For j = 0 To 7

dy_dm(i, j) = Adodc1.Recordset(j)

Next j

Adodc1.Recordset.MoveNext

Next i

'確定頁數

k = Int(zsl / 80)'80為每頁行數

If k - zsl / 80 < 0 Then

k = k + 1

End If

fnt = 8

For p = 0 To k - 1

pa = p + 1

y = 300

x = 4500

txt = "機物料庫存"

dd = prnt(x, y, fnt, txt)

x = 8500

txt = "第" & p + 1 & "頁"

dd = prnt(x, y, fnt, txt)

y = 500

x = 600

txt = "備件代碼"

dd = prnt(x, y, fnt, txt)

x = 1500

txt = "備件名稱"

dd = prnt(x, y, fnt, txt)

x = 3500

txt = "備件規格"

dd = prnt(x, y, fnt, txt)

x = 5500

txt = "進口計算機號"

dd = prnt(x, y, fnt, txt)

x = 7500

txt = "最低儲備量"

dd = prnt(x, y, fnt, txt)

x = 9000

txt = "庫存量"

dd = prnt(x, y, fnt, txt)

For i = 0 + p * 80 To 79 + p * 80

y = 700 + 180 * (i - p * 80)

x = 600

txt = dy_dm(i, 1)

dd = prnt(x, y, fnt, txt)

x = 1500

txt = dy_dm(i, 2)

dd = prnt(x, y, fnt, txt)

x = 3500

txt = dy_dm(i, 3)

dd = prnt(x, y, fnt, txt)

x = 5500

txt = dy_dm(i, 4)

dd = prnt(x, y, fnt, txt)

x = 7500

txt = dy_dm(i, 5)

dd = prnt(x, y, fnt, txt)

x = 9000

txt = dy_dm(i, 6)

dd = prnt(x, y, fnt, txt)

If Int(i / 10) - i / 10 = 0 Then

Printer.Line (500, 695 + (i - p * 80) * 180)-(10000, 695 + (i - p * 80) * 180)

End If

Next i

If zsl - p * 80 > 80 Then

Printer.Line (500, 695 + 180 * 80)-(10000, 695 + 180 * 80)

Printer.NewPage

End If

Next p

Printer.EndDoc ' 打印完成。

Cls

End Sub

Private Sub Form_Load()

Adodc1.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=jwl_dbf"

Adodc1.RecordSource = "select cs.js_dm,cs.備件代碼,cs.備件名稱,cs.備件規格,cs.進口計算機號,cs.最低庫存量,sl.結存數量,sl.結存金額,sl.結存單價,sl.類別代碼 from JWCK_BM as cs,jwl_jiec as sl" & _

" where cs.備件代碼 = sl.備件代碼" & " and cs.備件代碼>" & "''" & " order by sl.類別代碼,sl.備件代碼"

Adodc1.Refresh

DataGrid1.Refresh

zsl = Adodc1.Recordset.RecordCount

Adodc3.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=jwl_dbf"

Adodc3.RecordSource = "select sum(結存金額) as hj from jwl_jiec"

Adodc3.Refresh

End Sub

以上供參考。其它請參考西安交通大學出版社出版的“VISUAL BASIC 6.0 高級編程技巧-ADO數據訪問篇“

  • 上一篇:VHDL語言的特點
  • 下一篇:聰明媽媽,從不“講道理”
  • copyright 2024編程學習大全網