下面是一些示例代码,它使用OleDbConnection 和Interop.Excel 生成工资报告。我认为这在这里是相关的,因为返回的行可能有也可能没有所有列的值。报表在 Excel 中动态构建,省略了没有值的列。
Private Sub PayGrid_Report()
'PayGrid Report
If MessageBox.Show("Did you select a payperiod?",
"Just checking...",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then Exit Sub
Dim wb As Microsoft.Office.Interop.Excel.Workbook
Dim ws As Microsoft.Office.Interop.Excel.Worksheet
Dim xl As New Microsoft.Office.Interop.Excel.Application
'Create a save file dialog
Dim SaveFileDialog1 As SaveFileDialog
With SaveFileDialog1
.Filter = "Excel Workbooks|*.xlsx"
.AddExtension = True
.RestoreDirectory = True
.Title = "Save Report"
.OverwritePrompt = True
End With
'Ask the user where to save the file.
If SaveFileDialog1.ShowDialog() <> System.Windows.Forms.DialogResult.OK Then Exit Sub
Cursor = Cursors.WaitCursor 'spin the cursor so the user doesn't think it "froze"
'Set up the connection to the database
Dim dbConn As New System.Data.OleDb.OleDbConnection("Valid Connection String Here")
dbConn.Open()
Dim dbComm As New System.Data.OleDb.OleDbCommand
With dbComm
.Connection = dbConn
.CommandType = CommandType.StoredProcedure
.CommandText = "PayrollFunctions"
.Parameters.Add("PayPeriod", OleDbType.VarChar).Value = "2015P06" 'usually get a value from the form
.Parameters.Add("OutputType", OleDbType.Integer).Value = 4 'usually get a value from the form
End With
'start a data reader
Dim r As System.Data.OleDb.OleDbDataReader = dbComm.ExecuteReader(CommandBehavior.CloseConnection)
Dim rownum As Int32 = 0 'the row to write to in Excel
Dim t As Int32 'Top of each "report item" - used for formatting
Dim b As Int32 'Bottom of each "report item" - used for formatting
xl.Visible = True 'show Excel so the user can see the report building
wb = xl.Workbooks.Add() 'add a workbook to Excel
ws = wb.Sheets.Add 'add a sheet to the workbook
ws.Name = "PayGrid_Report" 'name the sheet
While r.Read()
rownum += 2
t = rownum
ws.Cells(rownum, 2) = r("EmployeeID")
ws.Cells(rownum, 3) = r("EmployeeName")
ws.Cells(rownum, 4) = r("PayrollDepartment")
ws.Range(ws.Cells(rownum, 2), ws.Cells(rownum, 4)).Font.Bold = True
If r("RegularHours") > 0 Then
rownum += 1
b = rownum
ws.Cells(rownum, 5) = "Regular Hours:"
ws.Cells(rownum, 6) = r("RegularHours")
ws.Cells(rownum, 7) = "@"
ws.Cells(rownum, 8) = r("RateReg")
ws.Cells(rownum, 9) = "="
ws.Cells(rownum, 10) = r("RegDollars")
End If
If r("OTHours") > 0 Then
rownum += 1
b = rownum
ws.Cells(rownum, 5) = "Overtime Hours:"
ws.Cells(rownum, 6) = r("OTHours")
ws.Cells(rownum, 7) = "@"
ws.Cells(rownum, 8) = r("RateOT")
ws.Cells(rownum, 9) = "="
ws.Cells(rownum, 10) = r("OTDollars")
End If
If r("LeaveHours") > 0 Then
rownum += 1
b = rownum
ws.Cells(rownum, 5) = "Vacation Hours:"
ws.Cells(rownum, 6) = r("LeaveHours")
ws.Cells(rownum, 7) = "@"
ws.Cells(rownum, 8) = r("RateVac")
ws.Cells(rownum, 9) = "="
ws.Cells(rownum, 10) = r("LeaveDollars")
End If
If r("HolidayHours") > 0 Then
rownum += 1
b = rownum
ws.Cells(rownum, 5) = "Holiday Hours:"
ws.Cells(rownum, 6) = r("HolidayHours")
ws.Cells(rownum, 7) = "@"
ws.Cells(rownum, 8) = r("RateHol")
ws.Cells(rownum, 9) = "="
ws.Cells(rownum, 10) = r("HolidayDollars")
End If
If r("OtherHours") > 0 Then
If r("RateOtherBas") > 0 Then
rownum += 1
b = rownum
ws.Cells(rownum, 5) = "Other Hours:"
ws.Cells(rownum, 6) = r("OtherHours")
ws.Cells(rownum, 7) = "@"
ws.Cells(rownum, 8) = r("RateOtherBas")
ws.Cells(rownum, 9) = "="
ws.Cells(rownum, 10) = r("OtherBaseDollars")
End If
If r("RateOtherHol") > 0 Then
rownum += 1
b = rownum
ws.Cells(rownum, 5) = "Other Holiday:"
ws.Cells(rownum, 6) = r("OtherHours")
ws.Cells(rownum, 7) = "@"
ws.Cells(rownum, 8) = r("RateOtherHol")
ws.Cells(rownum, 9) = "="
ws.Cells(rownum, 10) = r("OtherHolDollars")
End If
If r("RateOtherVac") > 0 Then
rownum += 1
b = rownum
ws.Cells(rownum, 5) = "Other Vacation:"
ws.Cells(rownum, 6) = r("OtherHours")
ws.Cells(rownum, 7) = "@"
ws.Cells(rownum, 8) = r("RateOtherVac")
ws.Cells(rownum, 9) = "="
ws.Cells(rownum, 10) = r("OtherVacDollars")
End If
If r("RateOtherBen") > 0 Then
rownum += 1
b = rownum
ws.Cells(rownum, 5) = "Other Benefits:"
ws.Cells(rownum, 6) = r("OtherHours")
ws.Cells(rownum, 7) = "@"
ws.Cells(rownum, 8) = r("RateOtherBen")
ws.Cells(rownum, 9) = "="
ws.Cells(rownum, 10) = r("OtherBenDollars")
End If
End If 'If r("OtherHours") > 0
rownum += 1
b = rownum
ws.Cells(rownum, 5) = "Total:"
ws.Cells(rownum, 6) = r("TotalHours")
ws.Cells(rownum, 10) = r("TotalDollars")
ws.Range(ws.Cells(rownum, 5), ws.Cells(rownum, 10)).Font.Bold = True
'create border around report item
Dim LS As Microsoft.Office.Interop.Excel.XlLineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
Dim BW As Microsoft.Office.Interop.Excel.XlBorderWeight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin
With ws.Range(ws.Cells(t, 2), ws.Cells(b, 10))
.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).LineStyle = LS
.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).Weight = BW
.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom).LineStyle = LS
.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom).Weight = BW
.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft).LineStyle = LS
.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft).Weight = BW
.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight).LineStyle = LS
.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight).Weight = BW
End With
End While
r.Close()
ws.Columns.AutoFit()
ws.Range(ws.Cells(1, 1), ws.Cells(1, 1)).ColumnWidth = 0.42
'Format the page setup
ws.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlPortrait
ws.PageSetup.FitToPagesWide = 1
ws.PageSetup.FitToPagesTall = 99
ws.PageSetup.LeftHeader = "Paygrid Report"
ws.PageSetup.CenterHeader = "Pay Period 06"
ws.PageSetup.RightHeader = "Page &P of &N"
ws.PageSetup.LeftFooter = "Generated " & Today.ToShortDateString
wb.SaveAs(SaveFileDialog1.FileName)
wb.Close()
xl.Quit()
Dim psi As New System.Diagnostics.ProcessStartInfo
psi.FileName = "excel"
psi.Arguments = """" & SaveFileDialog1.FileName & """"
Dim proc As System.Diagnostics.Process = System.Diagnostics.Process.Start(psi)
Cursor = Cursors.Default
End Sub