Public Class Form12
    Dim colwidth(0 To 18) As Single '第列的宽度,以1/10毫米为计量单位

    Private Sub VB2008Print1_PrintDocument() Handles p.PrintDocument

    End Sub

    Private Sub Form11_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        p.PageUnits = VBprinter.VB2008Print.PageExportUnit.CentiMeter '计量单位
        colwidth(0) = 100
        colwidth(1) = 220
        colwidth(2) = 100
        colwidth(3) = 150
        colwidth(4) = 100
        colwidth(5) = 160
        colwidth(6) = 170
        colwidth(7) = 180
        colwidth(8) = 100
        colwidth(9) = 160
        colwidth(10) = 100
        colwidth(11) = 160
        colwidth(12) = 100
        colwidth(13) = 100
        colwidth(14) = 100
        colwidth(15) = 160
        colwidth(16) = 100
        colwidth(17) = 160
        colwidth(18) = 170



        p.IsNeedCheckNewPage = False '

        p.PaperMargins = New System.Drawing.Printing.Margins(200, 100, 200, 100)
        p.NewDoc()
        p.NewPage(Drawing.Printing.PaperKind.A4, True)

        Dim footertxt As String = "制表：游平             中国航空动力机械研究所(注意到没,这是表尾的内容,每页都会重复打印哦)"
        Dim footerfont As Font = New Font("华文行楷", 13)
        Dim footerheight As Single
        footerheight = p.MeasureText(footertxt, footerfont, p.PaperPrintWidth).Height '每页表尾需要打印的高度




        Dim rows As Integer = 100 '这是测试数据，打印100行
        DrawTitle() '画标题
        DrawHeader() '画表头
        Dim h As Single = 60 '行高
        For i As Integer = 0 To rows
            DrawContent(i + 1, h) '画每行的内容
            p.Currenty = p.Currenty + h
            p.Currentx = 0
            If i = rows Then
                '此时是最后一行,

                '此时只要打印表尾就行了,不要再换页了
                DrawFooter(footertxt, footerfont, footerheight)

            Else
                '不是最后一行
                If p.IsNewPage(h + footerheight) = True Then
                    '要换页才能打印了

                    '先要打印表尾
                    DrawFooter(footertxt, footerfont, footerheight)
                    '再换页
                    p.NewPage(Drawing.Printing.PaperKind.A4, True)
                    DrawTitle() '画标题
                    DrawHeader() '画表头
                    '新页新打印标题和表头
                End If
            End If

        Next

        p.NewRow(20) '增加0.2厘米间隙,因为设置控件的PageUnits为CentiMeter
        p.DrawText("联系邮箱：myyouping@139.com  QQ：479781502(这里的内容只有表格最后一页的尾部打印一次哦)", p.PaperPrintWidth, New Font("宋体", 13))
        p.EndDoc()

    End Sub
    ''' <summary>
    ''' 每页画表尾
    ''' </summary>
    ''' <param name="txt">表尾内容</param>
    ''' <param name="fnt">表尾字体</param>
    ''' <param name="h">表尾打印所需要的高度</param>
    ''' <remarks></remarks>
    Private Sub DrawFooter(ByVal txt As String, ByVal fnt As Font, ByVal h As Single)
        p.DrawText(txt, p.PaperPrintWidth, h, fnt, Drawing.Color.Black, Drawing.StringAlignment.Near, Drawing.StringAlignment.Center, True, False, False, False, 0, 0)
        p.Currenty = p.Currenty + p.LastDrawTextHeight
        p.Currentx = 0
    End Sub
    ''' <summary>
    ''' 打印表格的内容
    ''' </summary>
    ''' <param name="i">第几行的数据</param>
    ''' <param name="h">行高</param>
    ''' <remarks></remarks>
    Private Sub DrawContent(ByVal i As Integer, ByVal h As Single)
        '画表格内容，这里仅测试,数据你应该是可以从自己的表格中取的

        Dim myf As New System.Drawing.Font("栺体_GB2312", 10)
        p.DrawCell(CStr(i), colwidth(0), h, myf, Drawing.Color.Black, "1101", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)

        p.DrawCell(CStr(i) & "部门", colwidth(1), h, myf, Drawing.Color.Black, "1101", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)

        Dim mys As String
        mys = "内容"
        p.DrawCell(mys, colwidth(2), h, myf, Drawing.Color.Black, "1101", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        p.DrawCell(mys, colwidth(3), h, myf, Drawing.Color.Black, "1101", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        p.DrawCell(mys, colwidth(4), h, myf, Drawing.Color.Black, "1101", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)

        p.DrawCell(mys, colwidth(5), h, myf, Drawing.Color.Black, "1101", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)

        p.DrawCell(mys, colwidth(6), h, myf, Drawing.Color.Black, "1101", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        p.DrawCell(mys, colwidth(7), h, myf, Drawing.Color.Black, "1101", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        p.DrawCell(mys, colwidth(8), h, myf, Drawing.Color.Black, "1101", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        p.DrawCell(mys, colwidth(9), h, myf, Drawing.Color.Black, "1101", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        p.DrawCell(mys, colwidth(10), h, myf, Drawing.Color.Black, "1101", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        p.DrawCell(mys, colwidth(11), h, myf, Drawing.Color.Black, "1101", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        p.DrawCell(mys, colwidth(12), h, myf, Drawing.Color.Black, "1101", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        p.DrawCell(mys, colwidth(13), h, myf, Drawing.Color.Black, "1101", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        p.DrawCell(mys, colwidth(14), h, myf, Drawing.Color.Black, "1101", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        p.DrawCell(mys, colwidth(15), h, myf, Drawing.Color.Black, "1101", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        p.DrawCell(mys, colwidth(16), h, myf, Drawing.Color.Black, "1101", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        p.DrawCell(mys, colwidth(17), h, myf, Drawing.Color.Black, "1101", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        p.DrawCell(mys, colwidth(18), h, myf, Drawing.Color.Black, "1111", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)



    End Sub
    ''' <summary>
    ''' 打印表格的标题行
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub DrawTitle()
        '打印标题
        Dim myf As System.Drawing.Font = New System.Drawing.Font("黑体", 18, Drawing.FontStyle.Bold)
        Dim txt As String
        txt = "自定义方式打印，复杂表头，每页都可以打印页眉与页脚，很灵活哦，换页效果也很好哦"
        p.DrawText(txt, p.PaperPrintWidth, p.MeasureText(txt, myf).Height, myf, Drawing.Color.Blue, Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 0, 0)
        p.NewRow(p.LastDrawTextHeight * 1.8) '


        myf = New System.Drawing.Font("宋体", 12, Drawing.FontStyle.Bold)
        txt = "这里可以打印表头等内容,内容多于一行也会自动换行的"
        p.DrawText(txt, p.PaperPrintWidth, p.MeasureText(txt, myf).Height, myf, Drawing.Color.Black, Drawing.StringAlignment.Near, Drawing.StringAlignment.Center, True, False, False, False, 0, 0)
        p.NewRow(p.LastDrawTextHeight * 1.5) '

    End Sub
    ''' <summary>
    '''   画表格的复杂表头
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub DrawHeader()
        '画复杂表头
        Dim myf As New System.Drawing.Font("宋体", 10)

        Dim h1 As Single = 60
        Dim h2 As Single = 60
        Dim h3 As Single = 60

        Dim mywidth As Single
        '先画第一层表头

        Dim tempx As Single
        Dim tempy As Single
        tempy = p.Currenty
        mywidth = colwidth(0) + colwidth(1) + colwidth(2) + colwidth(3)
        p.DrawCell("职员信息", mywidth, h1, myf, Drawing.Color.Black, "1101", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)

        mywidth = colwidth(4)
        p.DrawCell("出勤合计", mywidth, h1 + h2 + h3, myf, Drawing.Color.Black, "1111", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)


        mywidth = colwidth(5) + colwidth(6) + colwidth(7) + colwidth(8) + colwidth(9) + colwidth(10) + colwidth(11) + colwidth(12) + colwidth(13) + colwidth(14) + colwidth(15) + colwidth(16) + colwidth(17) + colwidth(18)
        p.DrawCell("各种工资", mywidth, h1, myf, Drawing.Color.Black, "0111", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        '画第二层表头
        p.Currenty = tempy + h1
        p.Currentx = 0
        mywidth = colwidth(0)
        p.DrawCell("部门号", mywidth, h2 + h3, myf, Drawing.Color.Black, "1001", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        mywidth = colwidth(1)
        p.DrawCell("部门名", mywidth, h2 + h3, myf, Drawing.Color.Black, "1001", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        mywidth = colwidth(2)
        p.DrawCell("在册人数", mywidth, h2 + h3, myf, Drawing.Color.Black, "1001", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        mywidth = colwidth(3)
        p.DrawCell("工资标准", mywidth, h2 + h3, myf, Drawing.Color.Black, "1001", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        mywidth = colwidth(4)
        p.DrawCell("", mywidth, h1 + h2 + h3, myf, Drawing.Color.Black, "0000", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        mywidth = colwidth(5)
        p.DrawCell("岗位工资", mywidth, h2 + h3, myf, Drawing.Color.Black, "1001", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        mywidth = colwidth(6)
        p.DrawCell("协议工资", mywidth, h2 + h3, myf, Drawing.Color.Black, "1001", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)

        mywidth = colwidth(7)
        p.DrawCell("其他工资", mywidth, h2 + h3, myf, Drawing.Color.Black, "1001", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)

        tempx = p.Currentx
        tempy = p.Currenty
        mywidth = colwidth(8) + colwidth(9)
        p.DrawCell("计件工资", mywidth, h2, myf, Drawing.Color.Black, "1001", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        p.Currentx = tempx
        p.Currenty = tempy + h2
        mywidth = colwidth(8)
        p.DrawCell("分数", mywidth, h3, myf, Drawing.Color.Black, "1001", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        mywidth = colwidth(9)
        p.DrawCell("工资", mywidth, h3, myf, Drawing.Color.Black, "1001", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        p.Currenty = tempy


        tempx = p.Currentx
        tempy = p.Currenty
        mywidth = colwidth(10) + colwidth(11)
        p.DrawCell("加班工资", mywidth, h2, myf, Drawing.Color.Black, "1001", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        p.Currentx = tempx
        p.Currenty = tempy + h2
        mywidth = colwidth(10)
        p.DrawCell("天数", mywidth, h3, myf, Drawing.Color.Black, "1001", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        mywidth = colwidth(11)
        p.DrawCell("工资", mywidth, h3, myf, Drawing.Color.Black, "1001", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)



        p.Currenty = tempy


        tempx = p.Currentx
        tempy = p.Currenty
        mywidth = colwidth(12) + colwidth(13) + colwidth(14) + colwidth(15)
        p.DrawCell("劳保工资", mywidth, h2, myf, Drawing.Color.Black, "1001", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        p.Currentx = tempx
        p.Currenty = tempy + h2
        mywidth = colwidth(12)
        p.DrawCell("工数", mywidth, h3, myf, Drawing.Color.Black, "1001", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        mywidth = colwidth(13)
        p.DrawCell("名", mywidth, h3, myf, Drawing.Color.Black, "1001", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        mywidth = colwidth(14)
        p.DrawCell("%率", mywidth, h3, myf, Drawing.Color.Black, "1001", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        mywidth = colwidth(15)
        p.DrawCell("工资", mywidth, h3, myf, Drawing.Color.Black, "1001", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)



        p.Currenty = tempy


        tempx = p.Currentx
        tempy = p.Currenty
        mywidth = colwidth(16) + colwidth(17)
        p.DrawCell("例假工资", mywidth, h2, myf, Drawing.Color.Black, "1001", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        p.Currentx = tempx
        p.Currenty = tempy + h2
        mywidth = colwidth(16)
        p.DrawCell("工数", mywidth, h3, myf, Drawing.Color.Black, "1001", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)
        mywidth = colwidth(17)
        p.DrawCell("工资", mywidth, h3, myf, Drawing.Color.Black, "1001", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)

        p.Currenty = tempy
        mywidth = colwidth(18)
        p.DrawCell("培训工资", mywidth, h2 + h3, myf, Drawing.Color.Black, "1011", Drawing.StringAlignment.Center, Drawing.StringAlignment.Center, True, False, False, False, 6, Drawing.Color.Transparent, Drawing.Color.Black)

        p.NewRow(h2 + h3)
    End Sub

    Private Sub p_HeaderFooterOut(ByVal pages As System.Int32, ByVal curpage As System.Int32) Handles p.HeaderFooterOut
        p.PrintFooter("联系QQ：479781502", "第" & CStr(curpage) & "页  共" & CStr(pages) & "页", "")
    End Sub

 
End Class
