Step 1 首先定義當鍵值不存在時新增鍵值之 SQL 指令
6.8 報表設計技巧
6.8.2 程式設計
6.8.2.4 啟動列印對話方塊
請在對應按鈕或下拉功能表之 Click 事件內加入列程式:
PrintDialog1.Document = Me.PrintDocument1
說明:將列印版面指向預設之PrintDocument物件 ↑H
If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then PrintDocument1.Print()
End If
6.8.2.4 設計 PrintDocument1_PrintPage 事件程序
請在此事件內加入下列程式碼,當設定 PrintPreviewDialog1.Document 或 PrintDialog1.Document 時,此事件程序就會被啟動來製作報表文件。
¾ 設定工作變數
Dim x As Single, y As Single, txtW As Single Dim txt
Dim dRow As DataRow
Dim fnd As Integer, str As String, i As Integer Dim gi As Integer, grpString As String
Dim prnX As Single, prnU As Single, maxPOS As Single = recWidth Dim pen As New Pen(Color.Black)
說明:建立畫筆
pgHight = e.MarginBounds.Height
說明:e 代表列印事件,讀取列印版面高度 pgWidth = e.MarginBounds.Width
說明:取出列印版面寬度 Dim docFont As Font
docFont = New Font(titleFontName, titleFontSize) 說明:設定字型
Dim docSize As New Size(e.MarginBounds.Width, e.MarginBounds.Height - docFont.GetHeight(e.Graphics))
說明:設定列印文件方塊大小 Dim lineRA As Single = 1 + spcLine
說明:設定每列文件佔用高度,spcLine表行距。
Dim docFormat As New StringFormat
docFormat.Trimming = StringTrimming.Word 說明:設定文字輸出格式,中文字不截斷 If recWidth < pgWidth Then
prnU = recWidth / lineLength Else
prnU = pgWidth / lineLength End If
x = e.MarginBounds.Left
說明:設定起始左邊界 ↑H
y = e.MarginBounds.Top 說明:設定起始出上邊界
¾ 列印報表抬頭
Dim rect As New Rectangle(x, y, pgWidth, docFont.GetHeight(e.Graphics)) e.Graphics.DrawString(rptTitle, docFont, Brushes.Black, rect, docFormat) y = y + docFont.GetHeight(e.Graphics) * lineRA
¾ 列印報表表頭
Call PrintHead(x, y, sender, e) 說明:請自行設計所需表頭
y = y + docFont.GetHeight(e.Graphics) * lineRA
¾ 列印欄位抬頭
Call PrintColHead(x, y, sender, e) 說明:請自行設計所需欄位抬頭
y = y + docFont.GetHeight(e.Graphics) * 2.2
¾ 列印報表本文-輸出所需紀錄
Do While True,說明:依序輸出來源資料之每筆紀錄
列印紀錄指標所在紀錄 For i = 0 To fln
txt = Trim(dRow.Item(i).ToString)
txtW = StrWidth(txt, dataFontName, dataFontSize) + 5 說明:StrWidth為自訂函數,用來量測文件寬度 If sumField(i) = True Then,說明:需要小計時 sumValue(i) = sumValue(i) + Val(txt) ttlValue(i) = ttlValue(i) + Val(txt) End If
If sumReport = False Then ‘非摘要式報表 If tblStruct(i).fldType = "Numeric" Then If i < fln Then
prnX = x + (colLOC(i + 1) - 2) * prnU - txtW Else
prnX = x + lineLength * prnU - txtW End If
rect = New Rectangle(prnX, y, txtW, docFont.GetHeight(e.Graphics)) e.Graphics.DrawString(txt, docFont, Brushes.Black, rect, docFormat)
Else ↑H
If tblStruct(i).fldType = "Date" Or tblStruct(i).fldType = "Stamp" Then txt = Trim(dRow.Item(i).ToString)
If daFMT = "Short" Then txt = Format(txt, "Short Date")
ElseIf tblStruct(i).fldType <> "Binary" And tblStruct(i).fldType <> "BLOB"
Then
txt = Trim(dRow.Item(i).ToString) Else
txt = "Byte_data"
說明:預設BLOB欄不印,並以Byte_data提示 End If
txtW = StrWidth(txt, dataFontName, dataFontSize) prnX = x + colLOC(i) * prnU
說明:colLOC(i)表預設欄位之X座標
rect = New Rectangle(prnX, y, txtW, docFont.GetHeight(e.Graphics)) e.Graphics.DrawString(txt, docFont, Brushes.Black, rect, docFormat) End If
Next
列印分隔線
If grdLine = True Then,說明:需要分隔線 y = y + docFont.GetHeight(e.Graphics) * lineRA e.Graphics.DrawLine(pen, gx(0), y, gx(fln + 1), y) Else
y = y + docFont.GetHeight(e.Graphics) * lineRA End If
移動紀錄指標
加入判斷次筆紀錄鍵值是否改變或已至檔尾 CurrentREC = CurrentREC + 1
dRow = rptTable.Rows(CurrentREC)
列印小計
If sumFLD = True Then,說明:需要小計 txt = "小計"
txtW = StrWidth(txt, dataFontName, dataFontSize) + 5 prnX = x + (colLOC(1) - 2) * prnU - txtW
rect = New Rectangle(prnX, y, txtW, docFont.GetHeight(e.Graphics)) e.Graphics.DrawString(txt, docFont, Brushes.Black, rect, docFormat) y = y + docFont.GetHeight(e.Graphics) * lineRA
For i = 1 To fln ↑H
If sumField(i) = True Then txt = Trim(CStr(sumValue(i)))
txtW = StrWidth(txt, dataFontName, dataFontSize) + 5 If i < fln Then
prnX = x + (colLOC(i + 1) - 2) * prnU - txtW Else
prnX = x + lineLength * prnU - txtW End If
rect = New Rectangle(prnX, y, txtW, docFont.GetHeight(e.Graphics)) e.Graphics.DrawString(txt, docFont, Brushes.Black, rect, docFormat) End If
Next End If
檔案尾端處理
Call PrintTail(x, y, sender, e) 說明:請自行加入報表表尾程式 e.HasMorePages = False
說明:已無資料 Exit Do
說明:跳出迴圈
文件區已滿且尚有資料錄
If y >= (e.MarginBounds.Bottom - docFont.GetHeight(e.Graphics) * 1.5) Then
Call PrintTail(x, y, sender, e) 說明:請自行加入報表表尾程式 curPage = curPage + 1
說明:頁碼加1 e.HasMorePages = True
說明:仍有資料 Exit Do
End If Loop
¾ 特殊指令說明
列印資料:e.Graphics.DrawString 畫線:e.Graphics.DrawLine 列印影像:e.Graphics.DrawImage
文件區已無資料:e.HasMorePages = False ↑H
文件區尚有資料:e.HasMorePages = True
中文字不截斷:docFormat.Trimming = StringTrimming.Word 6.9 條碼製作技巧
條碼常搭配表單或報表一起使用,可讓取得來源資料人員快速擷取內含鍵 值,從而取得原始內容。製作條碼時,必須先引用條碼控制項,引用方法如下:
¾ 開啟工具箱,
¾ 在工具箱上按下滑鼠右鍵以開啟快顯功能表,
¾ 點取【選擇項目】,開啟【工具箱選擇項目】對話控制項,
¾ 點取【COM元件】,捲動選項清單至【MicroSoft條碼控制項 9.0】,如圖 6.9,
¾ 點取【確定】,將條碼控制項加入工具箱。
圖 6.9 引入條碼控制項 6.9.1 加入條碼控制項
條碼必須搭配表單方能正常顯示,因此需要在表單內加入條碼控制項,接下 來可依所需條碼規格設定條碼屬性,工作畫面如圖 6.10。
↑H 圖 6.10 條碼屬性對話控制項
條碼屬性說明如下:
【樣式】-Style:可選用下列樣式:
0 - UPC-A 1 - UPC-B 2 - JAN-13 3 - JAN-8 4 - Case Code 5 - NW7 6 - Code-39 7 - Code-128 8 - U.S. Post net 9 - U.S. Postal FIM 10 - JP Post
【副樣式】-SubStyle:選擇樣式後,若有副樣式屬性,可依規格來設定,選項 內容如下:
0-UPC-A、1-UPC-A、2-JAN-13 及 3-JAN-8 有下列副樣式:
0-標準 1-2 位補充碼 2-5 位補充碼 3-POS Case Code
4-Case Code 有下列副樣式:
0-標準版 1-擴充版 2-附加版
8-U.S. Post net 有下列副樣式:
0-傳遞點 1-ZIP Code + 4 2-5 Digit ZIP Code
9-U.S. Post FIM 有下列副樣式:
0-FIM-A Symbol 1-FIM-B Symbol 2-FIM-C Symbol
【線條粗細】-LineWeight:可設定 1-5 之線條樣式。
【列印方向】-Direction:可設為下列形式:
0 - 0 度 1 - 90 度
2 - 180 度 ↑H
3 - 270 度
【標籤寬度】-Width:以 Pixel 為單位,可自行調整控制項。
【標籤高度】-Height:以 Pixel 為單位,可自行調整控制項。
可以使用程式來設定條碼屬性,語法如下:
barCD.Width = W barCD.Height = H barCD.Style =N barCD.SubStyle =M barCD.LineWeight = L barCD.Direction = D
barCD.Value =來源資料欄位
其中 barCD 為自定條碼控制項名稱。