2013-06-03 52 views
1

我有一個代碼正在從DataGridView打印銷售報告。代碼打印良好,但只有在控制變量I的值超出可打印區域時才能打印一頁。我真的需要你的幫助。以下是代碼。從DataGridView打印多個帶有循環的頁面

  Dim fntAddress As New Font("Comic Sans MS", 10, FontStyle.Regular) 
     Dim fntHeader As New Font("Calibri", 20, FontStyle.Bold) 
     Dim fntBodyText As New Font("Calibri", 12, FontStyle.Regular) 
     Dim fntHeaderText As New Font("Calibri", 13, FontStyle.Bold) 
     Dim strTotalSale = txtTotal.Text 

     e.Graphics.DrawString("SFC POINT OF SALE AND INVENTORY MANAGEMENT", fntHeader, Brushes.Black, 100, 0) 
     e.Graphics.DrawString("GENERATED SALES REPORT", New Font("Calibri", 18, FontStyle.Bold), Brushes.Black, 250, 30) 

     Dim strDateString As String = "" 
     If mtbStartDate.Text = "//" Or mtbEndDate.Text = "//" Then 
      strDateString = "" 
     ElseIf mtbStartDate.Text = mtbEndDate.Text Then 
      strDateString = "Report For Date Of : " & mtbStartDate.Text 
     ElseIf mtbStartDate.Text <> mtbEndDate.Text Then 
      strDateString = "Report For Dates Of : " & mtbStartDate.Text & " - " & mtbEndDate.Text 
     End If 

     e.Graphics.DrawString(strDateString, New Font("Courier New", 15, FontStyle.Regular), Brushes.Black, 5, 70) 

     e.Graphics.DrawRectangle(Pens.Black, New Rectangle(5, 100, 770, 35)) 
     e.Graphics.DrawString("Item Barcode", fntHeaderText, Brushes.Black, 10, 107) 
     e.Graphics.DrawString("Item Name", fntHeaderText, Brushes.Black, 160, 107) 
     e.Graphics.DrawString("Quantity", fntHeaderText, Brushes.Black, 360, 107) 
     e.Graphics.DrawString("Unit Cost", fntHeaderText, Brushes.Black, 450, 107) 
     e.Graphics.DrawString("Sub Total", fntHeaderText, Brushes.Black, 560, 107) 
     e.Graphics.DrawString("Date of Sale", fntHeaderText, Brushes.Black, 660, 107) 

     Dim RowCount As Integer = dgvSales.Rows.Count - 1 
     Static i As Integer = 139 
     Dim x1 = 10 
     Dim x2 = 700 
     Dim y1 = 155 
     Dim n As Integer = 0 


     While n < RowCount 
      e.Graphics.DrawRectangle(Pens.Black, New Rectangle(5, i - 5, 770, 35)) 
      e.Graphics.DrawString(dgvSales.Rows(n).Cells(0).Value, fntBodyText, Brushes.Black, 16, i) 
      e.Graphics.DrawString(dgvSales.Rows(n).Cells(1).Value, fntBodyText, Brushes.Black, 160, i) 
      e.Graphics.DrawString(dgvSales.Rows(n).Cells(2).Value, fntBodyText, Brushes.Black, 360, i) 
      e.Graphics.DrawString(dgvSales.Rows(n).Cells(3).Value, fntBodyText, Brushes.Black, 450, i) 
      e.Graphics.DrawString(dgvSales.Rows(n).Cells(4).Value, fntBodyText, Brushes.Black, 560, i) 
      e.Graphics.DrawString(dgvSales.Rows(n).Cells(5).Value, fntBodyText, Brushes.Black, 660, i) 
      i = i + 35 
      n = n + 1 
     End While 
     e.HasMorePages = False 
     e.Graphics.DrawLine(Pens.Black, 150, 100, 150, i - 5) 
     e.Graphics.DrawLine(Pens.Black, 350, 100, 350, i - 5) 
     e.Graphics.DrawLine(Pens.Black, 440, 100, 440, i - 5) 
     e.Graphics.DrawLine(Pens.Black, 550, 100, 550, i - 5) 
     e.Graphics.DrawLine(Pens.Black, 650, 100, 650, i - 5) 

     e.Graphics.DrawRectangle(Pens.Black, New Rectangle(340, i + 40, 174, 50)) 
     e.Graphics.DrawRectangle(Pens.Black, New Rectangle(341, i + 41, 172, 48)) 
     e.Graphics.DrawRectangle(Pens.Black, New Rectangle(342, i + 42, 170, 46)) 
     e.Graphics.DrawRectangle(Pens.Black, New Rectangle(343, i + 43, 168, 44)) 
     e.Graphics.DrawString("Total Sales.", New Font("Times New Roman (Headings CS)", 22, FontStyle.Bold), Brushes.Black, 341, i + 47) 

     e.Graphics.DrawRectangle(Pens.Black, New Rectangle(525, i + 40, 250, 50)) 
     e.Graphics.DrawRectangle(Pens.Black, New Rectangle(526, i + 41, 248, 48)) 
     e.Graphics.DrawRectangle(Pens.Black, New Rectangle(527, i + 42, 246, 46)) 
     e.Graphics.DrawRectangle(Pens.Black, New Rectangle(528, i + 43, 244, 44)) 
     e.Graphics.DrawString(strTotalSale, New Font("Times New Roman (Headings CS)", 23, FontStyle.Bold), Brushes.Black, 538, i + 47) 
+0

Static i As Integer = 139 ..那是什麼意思? – matzone

回答

0

老實說,我沒有寫在我的生活中VB一行代碼,而且一目瞭然印刷機構似乎非常類似於Java打印API。

您必須使用e.hasMorePages = True來標記您正在打印的文檔有更多頁面的事實。

這裏是一個工作示例: http://www.dreamincode.net/forums/topic/139447-help-with-hasmorepages-please/

請考慮到這個事實,你必須知道你離開了前者頁面上的位置。這裏是另一個,更現實的例子:http://www.dreamincode.net/forums/topic/128639-ehasmorepages-and-logic-to-use-it/

0

希望你明白,這可能是複雜的,因爲你算算fontheight,紙張大小高度等

在這種情況下,可以考慮將有一個固定的設置

Dim nMaxLine as Integer = 30 ' ----------> lines count per page 
Dim x as Integer 
Dim n as Integer 

For x = 0 to datagridview.rows.count - 1 
    ' .. 
    e.Graphics.DrawString(...) 
    ' .. 
    n += 1 
    If n > nMaxLine Then 
    e.hasMorePages = True 
    n = 0 
    End If 
Next