Option Strict Off Imports System.Data.SqlClient 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 Public Class 列印_零用金支付明細 Dim conn As New SqlConnection Dim da As New SqlDataAdapter Dim cmd As New SqlCommand Dim ds As New DataSet : Dim ds1 As New DataSet Dim dr As SqlDataReader Dim N1 As Integer : Dim N2 As Integer Dim xlApp As Microsoft.Office.Interop.Excel.Application Dim xlBook As Microsoft.Office.Interop.Excel.Workbook Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet Private Sub Set_DGV1載入前設定() DataGridView1.DataSource = Nothing : ds.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(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close() End Sub Private Sub Set_清單() Set_DGV1載入前設定() If CheckBox1.Checked = True Then SQL1 = 列印用SQL Else SQL1 = "SELECT 零用金支付表.流水號, 零用金支付表.部門, 零用金支付表.申請人, LEFT(零用金支付表.申請日期, 10) AS 申請日期, 零用金支付表.審核人, LEFT(零用金支付表.審核日期, 10) AS 審核日期, " & "零用金支付表.核准人, LEFT(零用金支付表.核准日期, 10) AS 核准日期, 零用金支付明細.項次, 零用金支付明細.用途, 零用金支付明細.金額, 零用金支付明細.會計 AS 會計作業, " & "零用金支付明細.出納 AS 出納作業, 零用金支付明細.備註, 零用金支付表.會計, 零用金支付表.作帳日期, 零用金支付表.出納, 零用金支付表.出納日期, 零用金支付明細.現金, 零用金支付明細.備用金, " & "零用金支付明細.公帳 FROM 零用金支付表 RIGHT OUTER JOIN 零用金支付明細 " & "ON 零用金支付表.流水號 = 零用金支付明細.流水號 WHERE (零用金支付表.審核人 NOT LIKE N'作廢%') AND (零用金支付表.核准人 NOT LIKE N'') AND (零用金支付表.核准人 NOT LIKE N'作廢%') " & "AND (零用金支付表.核准日期 LIKE N'" & ComboBox1.Text & "%') " & " ORDER BY 零用金支付表.流水號 DESC, 零用金支付明細.項次" End If Set_DGV1載入後設定() End Sub Private Sub Set_格式設定() DataGridView1.Visible = False End Sub Private Sub ComboBox1下拉表單資料載入() conn.Close() If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If SQL1 = "SELECT 年份 FROM 年份清單 ORDER BY 年份 DESC" cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader ComboBox1.Items.Clear() While (dr.Read()) : ComboBox1.Items.Add(dr("年份")) : End While conn.Close() End Sub Private Sub 列印_零用金支付明細_Load(sender As Object, e As EventArgs) Handles MyBase.Load FormBorderStyle = FormBorderStyle.SizableToolWindow : ControlBox = False ComboBox1下拉表單資料載入() CheckBox1.Checked = True : CheckBox2.Checked = False Set_清單() : Set_格式設定() End Sub Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click xlApp = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.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, 2) = "部門" : xlSheet.Cells(2, 3) = "申請人" : xlSheet.Cells(2, 4) = "申請日期" xlSheet.Cells(2, 5) = "審核人" : xlSheet.Cells(2, 6) = "審核日期" : xlSheet.Cells(2, 7) = "核准人" : xlSheet.Cells(2, 8) = "核准日期" : xlSheet.Cells(2, 9) = "項次" xlSheet.Cells(2, 10) = "用途" : xlSheet.Cells(2, 11) = "金額" : xlSheet.Cells(2, 12) = "備註" : xlSheet.Cells(2, 13) = "會計" : xlSheet.Cells(2, 14) = "作帳日期" : xlSheet.Cells(2, 15) = "出納" xlSheet.Cells(2, 16) = "出納日期" : xlSheet.Cells(2, 17) = "現金" : xlSheet.Cells(2, 18) = "備用金" : xlSheet.Cells(2, 19) = "公帳" AA(xlApp, xlSheet) For i As Integer = 0 To DataGridView1.Rows.Count - 1 xlSheet.Cells(i + 3, 1) = DataGridView1.Rows(i).Cells("流水號").Value : xlSheet.Cells(i + 3, 2) = DataGridView1.Rows(i).Cells("部門").Value xlSheet.Cells(i + 3, 3) = DataGridView1.Rows(i).Cells("申請人").Value : xlSheet.Cells(i + 3, 4) = DataGridView1.Rows(i).Cells("申請日期").Value xlSheet.Cells(i + 3, 5) = DataGridView1.Rows(i).Cells("審核人").Value : xlSheet.Cells(i + 3, 6) = DataGridView1.Rows(i).Cells("審核日期").Value xlSheet.Cells(i + 3, 7) = DataGridView1.Rows(i).Cells("核准人").Value : xlSheet.Cells(i + 3, 8) = DataGridView1.Rows(i).Cells("核准日期").Value xlSheet.Cells(i + 3, 9) = DataGridView1.Rows(i).Cells("項次").Value : xlSheet.Cells(i + 3, 10) = DataGridView1.Rows(i).Cells("用途").Value xlSheet.Cells(i + 3, 11) = DataGridView1.Rows(i).Cells("金額").Value : xlSheet.Cells(i + 3, 12) = DataGridView1.Rows(i).Cells("備註").Value xlSheet.Cells(i + 3, 13) = DataGridView1.Rows(i).Cells("會計").Value : xlSheet.Cells(i + 3, 14) = DataGridView1.Rows(i).Cells("作帳日期").Value xlSheet.Cells(i + 3, 15) = DataGridView1.Rows(i).Cells("出納").Value : xlSheet.Cells(i + 3, 16) = DataGridView1.Rows(i).Cells("出納日期").Value xlSheet.Cells(i + 3, 17) = DataGridView1.Rows(i).Cells("現金").Value : xlSheet.Cells(i + 3, 18) = DataGridView1.Rows(i).Cells("備用金").Value xlSheet.Cells(i + 3, 19) = DataGridView1.Rows(i).Cells("公帳").Value With xlApp.Selection .Font.Bold = True End With N2 = i + 3 : N1 = i Next i BB(xlApp, xlSheet) xlSheet.PageSetup.PrintArea = "" xlApp.Cells.Select() xlSheet.Range("B1").Select() xlApp.Application.WindowState = xlMinimized 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 Microsoft.Office.Interop.Excel.Workbook) As Microsoft.Office.Interop.Excel.Worksheet Return CType(xlBook.Worksheets.Add, Microsoft.Office.Interop.Excel.Worksheet) End Function Private Sub AA(ByVal myExcel As Microsoft.Office.Interop.Excel.Application, ByVal xlSheet As Microsoft.Office.Interop.Excel.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.Rows("1:1").Select : myExcel.Selection.RowHeight = 30 xlSheet.Rows("2:2").Select : myExcel.Selection.RowHeight = 25 With myExcel.Selection : .HorizontalAlignment = xlGeneral : .VerticalAlignment = xlTop : .WrapText = False : .Orientation = 0 : .AddIndent = False : .IndentLevel = 0 : .ShrinkToFit = False .ReadingOrder = xlContext : .MergeCells = False : End With myExcel.Selection.AutoFilter xlSheet.Range("A1").Select() 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.Rows("3:3").Select myExcel.ActiveWindow.FreezePanes = True xlSheet.Range("A2:S2").Select() myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With With myExcel.Selection.Borders(xlInsideVertical) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With With myExcel.Selection.Borders(xlInsideHorizontal) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With myExcel.ActiveWindow.DisplayGridlines = False xlSheet.Columns("A:A").Select : myExcel.Selection.ColumnWidth = 15.5 xlSheet.Columns("B:B").Select : myExcel.Selection.ColumnWidth = 11.5 xlSheet.Columns("C:C").Select : myExcel.Selection.ColumnWidth = 6 xlSheet.Columns("D:D").Select : myExcel.Selection.NumberFormatLocal = "yyyy/mm/dd" : myExcel.Selection.ColumnWidth = 10 xlSheet.Columns("E:E").Select : myExcel.Selection.ColumnWidth = 6 xlSheet.Columns("F:F").Select : myExcel.Selection.NumberFormatLocal = "yyyy/mm/dd" : myExcel.Selection.ColumnWidth = 10 xlSheet.Columns("G:G").Select : myExcel.Selection.ColumnWidth = 6 xlSheet.Columns("H:H").Select : myExcel.Selection.NumberFormatLocal = "yyyy/mm/dd" : myExcel.Selection.ColumnWidth = 10 xlSheet.Columns("I:I").Select : myExcel.Selection.ColumnWidth = 4 : myExcel.Selection.NumberFormatLocal = "@" xlSheet.Columns("J:J").Select : myExcel.Selection.ColumnWidth = 52 xlSheet.Columns("K:K").Select : myExcel.Selection.ColumnWidth = 12 : myExcel.Selection.Style = "Comma" xlSheet.Columns("L:L").Select : myExcel.Selection.ColumnWidth = 45 xlSheet.Columns("M:M").Select : myExcel.Selection.ColumnWidth = 6 xlSheet.Columns("N:N").Select : myExcel.Selection.NumberFormatLocal = "yyyy/mm/dd" : myExcel.Selection.ColumnWidth = 10 xlSheet.Columns("O:O").Select : myExcel.Selection.ColumnWidth = 6 xlSheet.Columns("P:P").Select : myExcel.Selection.NumberFormatLocal = "yyyy/mm/dd" : myExcel.Selection.ColumnWidth = 10 xlSheet.Columns("Q:S").Select : myExcel.Selection.ColumnWidth = 8 xlSheet.Cells.Select() : myExcel.ActiveWindow.Zoom = 90 End Sub Private Sub BB(ByVal myExcel As Microsoft.Office.Interop.Excel.Application, ByVal xlSheet As Microsoft.Office.Interop.Excel.Worksheet) Dim 位置1 As String 位置1 = "A3:S" & N1 + 3 : xlSheet.Range(位置1).Select() myExcel.Selection.Borders(xlDiagonalDown).LineStyle = xlNone : myExcel.Selection.Borders(xlDiagonalUp).LineStyle = xlNone With myExcel.Selection.Borders(xlEdgeLeft) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With With myExcel.Selection.Borders(xlEdgeTop) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With With myExcel.Selection.Borders(xlEdgeBottom) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With With myExcel.Selection.Borders(xlEdgeRight) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With With myExcel.Selection.Borders(xlInsideVertical) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With With myExcel.Selection.Borders(xlInsideHorizontal) : .LineStyle = xlContinuous : .ColorIndex = 0 : .TintAndShade = 0 : .Weight = xlThin : End With End Sub Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.Click CheckBox1.Checked = True : CheckBox2.Checked = False Set_清單() End Sub Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.Click CheckBox1.Checked = False : CheckBox2.Checked = True Set_清單() End Sub Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged Set_清單() End Sub End Class