Option Strict Off
Imports System.Data.SqlClient
Imports System.IO
Imports Microsoft.Office.Interop.Excel.XlUnderlineStyle
Imports Microsoft.Office.Interop.Excel.Constants
Imports Microsoft.Office.Interop.Excel.XlBordersIndex
Imports Microsoft.Office.Interop.Excel.XlLineStyle
Imports Microsoft.Office.Interop.Excel.XlBorderWeight
Imports Microsoft.Office.Interop.Excel.XlThemeFont
Imports Microsoft.Office.Interop.Excel.XlThemeColor
Imports Microsoft.Office.Interop.Excel.XlWindowState
Imports Microsoft.Office.Interop.Excel
Public Class 列印_零用金支付單
    Dim conn As New SqlConnection
    Dim da As New SqlDataAdapter
    Dim cmd As New SqlCommand
    Dim ds1 As New DataSet : Dim ds2 As New DataSet : Dim ds3 As New DataSet : Dim ds4 As New DataSet
    Dim dr As SqlDataReader
    Dim N1 As Integer : Dim N2 As Integer
    Dim xlApp As Application
    Dim xlBook As Workbook
    Dim xlSheet As Worksheet
    Dim 位置1 As String
    Private Sub Set_DGV1載入前設定()
        DataGridView1.DataSource = Nothing : ds1.Clear()
        DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView1.ColumnHeadersHeight = 25
        DataGridView1.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV1載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds1) : DataGridView1.DataSource = ds1.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_訂單清單1()
        Set_DGV1載入前設定()
        SQL1 = "SELECT 零用金支付表.流水號, 零用金支付表.部門, 零用金支付表.申請人, LEFT(零用金支付表.申請日期, 10) AS 申請日期, 
                           零用金支付表.審核人, LEFT(零用金支付表.審核日期, 10) AS 審核日期, 零用金支付表.核准人, 
                           LEFT(零用金支付表.核准日期, 10) AS 核准日期, 零用金支付表.核准圖檔, 零用金支付表.簽名編碼, 
                           零用金支付明細.出納 AS 已出納
                    FROM 零用金支付表 INNER JOIN
                           零用金支付明細 ON 零用金支付表.流水號 = 零用金支付明細.流水號
                    WHERE (零用金支付表.流水號 LIKE N'" & 列印用SQL & "') AND (零用金支付表.審核人 NOT LIKE N'作廢%') AND (零用金支付表.審核人 NOT LIKE N'') AND 
                          (零用金支付表.核准人 NOT LIKE N'作廢%') AND (零用金支付表.核准人 NOT LIKE N'')
                    GROUP BY 零用金支付表.流水號, 零用金支付表.部門, 零用金支付表.申請人, LEFT(零用金支付表.申請日期, 10), 
                            零用金支付表.審核人, LEFT(零用金支付表.審核日期, 10), 零用金支付表.核准人, LEFT(零用金支付表.核准日期, 10), 
                            零用金支付表.核准圖檔, 零用金支付表.簽名編碼, 零用金支付明細.出納
                    ORDER BY 零用金支付表.流水號 DESC"
        Set_DGV1載入後設定()
    End Sub
    Private Sub Set_DGV2載入前設定()
        DataGridView2.DataSource = Nothing : ds2.Clear()
        DataGridView2.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView2.ColumnHeadersHeight = 25
        DataGridView2.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_訂單清單2()
        Set_DGV2載入前設定()
        SQL1 = 列印用SQL2
        Set_DGV2載入後設定()
    End Sub
    Private Sub Set_DGV2載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds2) : DataGridView2.DataSource = ds2.Tables(0) : conn.Close()
    End Sub
    Private Sub Set_格式設定()
        DataGridView1.Visible = False : DataGridView2.Visible = False
        PictureBox1.Visible = False
    End Sub
    Private Sub 列印_零用金支付單_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Set_格式設定()
        FormBorderStyle = FormBorderStyle.SizableToolWindow : ControlBox = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 簽名檔圖片 FROM 簽名檔管理 WHERE 圖片流水號 LIKE N'" & 簽核圖片編號 & "'"
        cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
        While dr.Read() = True
            Dim bytes As Byte() = New Byte(-1) {}
            bytes = DirectCast(dr.Item("簽名檔圖片"), Byte())
            Dim oStream As New MemoryStream(bytes)
            PictureBox1.Image = Bitmap.FromStream(oStream)
        End While
        conn.Close()
        Set_訂單清單1() : Set_訂單清單2()
    End Sub
    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        xlApp = CType(CreateObject("Excel.Application"), Application)
        xlBook = xlApp.Workbooks.Add
        xlApp.DisplayAlerts = True
        xlBook.Activate()
        xlSheet = NewMethod(xlBook)
        xlSheet.Activate()
        xlApp.Visible = True
        xlApp.Application.WindowState = xlMaximized

        xlSheet.Cells(1, 1) = "費用報銷單" : xlSheet.Cells(2, 1) = "報銷部門 : " : xlSheet.Cells(2, 4) = "申請人 : " : xlSheet.Cells(2, 6) = "申請日期 : " : xlSheet.Cells(3, 1) = "項次"
        xlSheet.Cells(3, 2) = "用途" : xlSheet.Cells(3, 5) = "金額" : xlSheet.Cells(3, 6) = "備註"
        xlSheet.Cells(2, 3) = DataGridView1.Rows(0).Cells("部門").Value : xlSheet.Cells(2, 5) = DataGridView1.Rows(0).Cells("申請人").Value
        xlSheet.Cells(2, 7) = DataGridView1.Rows(0).Cells("申請日期").Value

        For i As Integer = 0 To DataGridView2.Rows.Count - 1
            xlSheet.Cells(i + 4, 1) = i + 1
            xlSheet.Cells(i + 4, 2) = DataGridView2.Rows(i).Cells("用途").Value
            xlSheet.Cells(i + 4, 5) = DataGridView2.Rows(i).Cells("金額").Value
            xlSheet.Cells(i + 4, 6) = DataGridView2.Rows(i).Cells("備註").Value
            With xlApp.Selection
                .Font.Bold = True
            End With
            N1 = i + 4 : N2 = i + 4
        Next i

        xlSheet.Cells(N1 + 1, 2) = "以下空白" : xlSheet.Cells(N1 + 3, 4) = "合计" : xlSheet.Cells(N1 + 3, 5) = "=SUM(E4:E" & N1 + 1 & ")"
        xlSheet.Cells(N1 + 3, 1) = "單據驗證碼 : " : xlSheet.Cells(N1 + 4, 1) = "核准人 : " : xlSheet.Cells(N1 + 4, 4) = "審核人 : " : xlSheet.Cells(N1 + 4, 6) = "審核日期 : "
        xlSheet.Cells(N1 + 5, 4) = "會計 : " : xlSheet.Cells(N1 + 5, 6) = "對帳日期 : " : xlSheet.Cells(N1 + 6, 4) = "出納 : " : xlSheet.Cells(N1 + 6, 6) = "出納日期 : "
        xlSheet.Cells(N1 + 6, 8) = "領款人 : "
        xlSheet.Cells(N1 + 3, 3) = DataGridView1.Rows(0).Cells("簽名編碼").Value : xlSheet.Cells(N1 + 4, 5) = DataGridView1.Rows(0).Cells("審核人").Value
        xlSheet.Cells(N1 + 4, 7) = DataGridView1.Rows(0).Cells("審核日期").Value

        AA(xlApp, xlSheet) : BB(xlApp, xlSheet) : CC(xlApp, xlSheet)

        xlSheet.PageSetup.PrintArea = ""
        xlApp.Cells.Select()
        xlSheet.Range("B1").Select()
        xlApp.Application.WindowState = xlMinimized
        xlApp.ActiveSheet.Protect(DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="0911274990")
        xlApp.ActiveSheet.EnableSelection = 1
        MsgBox("列印完成")
        Me.Close()
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        列印用SQL = "" : Me.Close()
    End Sub
    Private Shared Function NewMethod(xlBook As Workbook) As Worksheet
        Return CType(xlBook.Worksheets.Add, Worksheet)
    End Function
    Private Sub AA(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
        xlSheet.Cells.Select()
        With myExcel.Selection.Font : .Name = "微軟正黑體" : .Size = 10 : .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ThemeColor = xlThemeColorLight1
            .TintAndShade = 0
            .ThemeFont = xlThemeFontNone
        End With
        xlSheet.Columns("A:A").Select : myExcel.Selection.ColumnWidth = 5
        xlSheet.Columns("B:B").Select : myExcel.Selection.ColumnWidth = 9
        xlSheet.Columns("C:C").Select : myExcel.Selection.ColumnWidth = 25
        xlSheet.Columns("D:D").Select : myExcel.Selection.ColumnWidth = 9
        xlSheet.Columns("E:E").Select : myExcel.Selection.ColumnWidth = 15
        xlSheet.Columns("F:F").Select : myExcel.Selection.ColumnWidth = 10
        xlSheet.Columns("G:G").Select : myExcel.Selection.ColumnWidth = 12
        xlSheet.Columns("H:H").Select : myExcel.Selection.ColumnWidth = 8
        xlSheet.Columns("I:I").Select : myExcel.Selection.ColumnWidth = 10
        xlSheet.Rows("1:1").Select : myExcel.Selection.RowHeight = 35
        xlSheet.Rows("2:2").Select : myExcel.Selection.RowHeight = 30
        xlSheet.Rows("3:3").Select : myExcel.Selection.RowHeight = 20
        xlSheet.Rows("4:" & N2 + 2 & "").Select : myExcel.Selection.RowHeight = 30
        xlSheet.Rows("" & N2 + 3 & ":" & N2 + 6 & "").Select : myExcel.Selection.RowHeight = 30

        位置1 = "A3:I" & N2 + 2 : xlSheet.Range(位置1).Select()
        myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = xlAutomatic : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = xlAutomatic : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = xlAutomatic : .TintAndShade = 0 : .Weight = xlThin : End With
        With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = xlAutomatic : .TintAndShade = 0 : .Weight = xlThin : End With
        myExcel.Selection.Borders(xlInsideVertical).LineStyle = xlNone
        With myExcel.Selection.Borders(xlInsideHorizontal) : .LineStyle = xlContinuous : .ColorIndex = xlAutomatic : .TintAndShade = 0 : .Weight = xlThin : End With

        xlSheet.Range("A1:I1").Select() : myExcel.Selection.Merge
        With myExcel.Selection.Font : .Name = "微軟正黑體" : .Size = 20 : .Strikethrough = False : .Superscript = False : .Subscript = False : .OutlineFont = False : .Shadow = False
            .Underline = xlUnderlineStyleNone : .ThemeColor = xlThemeColorLight1 : .TintAndShade = 0 : .ThemeFont = xlThemeFontNone : End With

        xlSheet.Range("A2:I2").Select()
        With myExcel.Selection : .HorizontalAlignment = xlGeneral : .VerticalAlignment = xlBottom : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
        myExcel.ActiveWindow.DisplayGridlines = False

        位置1 = "A2:A" & N2 + 4 : xlSheet.Range(位置1).Select()
        With myExcel.Selection : .HorizontalAlignment = xlLeft : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
            .ReadingOrder = xlContext : .MergeCells = False : End With

        位置1 = "D2:D" & N2 + 4 : xlSheet.Range(位置1).Select()
        With myExcel.Selection : .HorizontalAlignment = xlLeft : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
            .ReadingOrder = xlContext : .MergeCells = False : End With

        位置1 = "E2:E" & N2 + 4 : xlSheet.Range(位置1).Select()
        myExcel.Selection.Style = "Comma"
        With myExcel.Selection : .HorizontalAlignment = xlRight : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
            .ReadingOrder = xlContext : .MergeCells = False : End With

        xlSheet.Range("E2").Select()
        With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlBottom : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With

        位置1 = "E" & N2 + 4 : xlSheet.Range(位置1).Select()
        With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With

        位置1 = "G" & N2 + 4 : xlSheet.Range(位置1).Select()
        With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With

        位置1 = "F" & N2 + 4 : xlSheet.Range(位置1).Select()
        With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With

        位置1 = "F" & N2 + 5 : xlSheet.Range(位置1).Select()
        With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With

        位置1 = "F" & N2 + 6 : xlSheet.Range(位置1).Select()
        With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With

        位置1 = "H" & N2 + 4 : xlSheet.Range(位置1).Select()
        With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With

        xlSheet.Range("G2").Select()
        With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With

        xlSheet.Range("F2").Select()
        With myExcel.Selection : .HorizontalAlignment = xlRight : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
            .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With

        xlSheet.Range("A1:I1").Select()
        With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .WrapText = False : .Orientation = 0 : .AddIndent = False
            .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = True : End With

        xlSheet.Range("F2:G2").Select()
        With myExcel.Selection : .VerticalAlignment = xlBottom : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False
            .ReadingOrder = xlContext : .MergeCells = False : End With
        For I As Integer = 4 To N2
            xlSheet.Range("B" & I & ":D" & I).Select()
            With myExcel.Selection : .HorizontalAlignment = xlCenter : .VerticalAlignment = xlCenter : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0
                .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = False : End With
            myExcel.Selection.Merge
            With myExcel.Selection : .HorizontalAlignment = xlLeft : .VerticalAlignment = xlCenter : .WrapText = True : .Orientation = 0 : .AddIndent = False
                .IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder = xlContext : .MergeCells = True : End With
        Next
    End Sub
    Private Sub BB(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
        Clipboard.SetDataObject(PictureBox1.Image)
        位置1 = "C" & N2 + 4 : xlSheet.Range(位置1).Select()
        myExcel.ActiveSheet.Pictures.Paste.Select
        myExcel.Selection.ShapeRange.Height = 43
    End Sub
    Private Sub CC(ByVal myExcel As Application, ByVal xlSheet As Worksheet)
        myExcel.Application.PrintCommunication = False
        With myExcel.ActiveSheet.PageSetup
            .PrintTitleRows = ""
            .PrintTitleColumns = ""
        End With
        myExcel.Application.PrintCommunication = True
        myExcel.ActiveSheet.PageSetup.PrintArea = ""
        myExcel.Application.PrintCommunication = False
        With myExcel.ActiveSheet.PageSetup
            .LeftHeader = ""
            .CenterHeader = ""
            .RightHeader = ""
            .LeftFooter = ""
            .CenterFooter = ""
            .RightFooter = ""
            .LeftMargin = myExcel.Application.InchesToPoints(0.25)
            .RightMargin = myExcel.Application.InchesToPoints(0.25)
            .TopMargin = myExcel.Application.InchesToPoints(0.75)
            .BottomMargin = myExcel.Application.InchesToPoints(0.75)
            .HeaderMargin = myExcel.Application.InchesToPoints(0.3)
            .FooterMargin = myExcel.Application.InchesToPoints(0.3)
            .PrintHeadings = False
            .PrintGridlines = False
            .PrintQuality = 600
            .CenterHorizontally = False
            .CenterVertically = False
            .Draft = False
            .FirstPageNumber = xlAutomatic
            .BlackAndWhite = False
            .Zoom = False
            .FitToPagesWide = 1
            .FitToPagesTall = 1
            .OddAndEvenPagesHeaderFooter = False
            .DifferentFirstPageHeaderFooter = False
            .ScaleWithDocHeaderFooter = True
            .AlignMarginsHeaderFooter = True
            .EvenPage.LeftHeader.Text = ""
            .EvenPage.CenterHeader.Text = ""
            .EvenPage.RightHeader.Text = ""
            .EvenPage.LeftFooter.Text = ""
            .EvenPage.CenterFooter.Text = ""
            .EvenPage.RightFooter.Text = ""
            .FirstPage.LeftHeader.Text = ""
            .FirstPage.CenterHeader.Text = ""
            .FirstPage.RightHeader.Text = ""
            .FirstPage.LeftFooter.Text = ""
            .FirstPage.CenterFooter.Text = ""
            .FirstPage.RightFooter.Text = ""
        End With
        myExcel.Application.PrintCommunication = True
    End Sub
End Class