16,552
社区成员
发帖
与我相关
我的任务
分享
Imports System.Data
Imports System.Data.Common
Imports System.Windows.Forms.DateTimePicker
Imports System.Configuration
Imports System.Reflection.MethodBase
Imports System.Text
Imports System.Data.SqlClient '引用數據庫服務
Imports System.Reflection
Imports Microsoft.Office.Interop
Public Class frmPrint
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim strSql As String = ""
Dim ds As New DataSet
Dim strMsg As String = ""
'从第五行开始打印正文
Dim i As Integer = 4
Dim j As Integer
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Try
'清空dataset
ds.Clear()
'创建Excel 对象
xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
xlBook = CType(xlApp.Workbooks.Add, Excel.Workbook)
xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet)
'打印报表头
xlSheet.Range("A1:G1").Select()
With xlSheet.Range("A1:G1").Select
xlSheet.Range("A1:G1").MergeCells = True
'xlSheet.Range("A1:G1").HorizontalAlignment = xlCenter
'xlSheet.Range("A1:G1").VerticalAlignment = .xlCenter
End With
xlSheet.Range("A1:G1").Value = "員工信息报表"
'打印页眉
xlSheet.Cells(3, 1) = "员工编号"
xlSheet.Cells(3, 2) = "员工名称"
xlSheet.Cells(3, 3) = "性别"
xlSheet.Cells(3, 4) = "身份证号码"
xlSheet.Cells(3, 5) = "出生日期"
xlSheet.Cells(3, 6) = "年龄"
xlSheet.Cells(3, 7) = "部门编号"
xlSheet.Cells(3, 8) = "部门名称"
xlSheet.Cells(3, 9) = "上级部门"
If chkChooes.Checked = True And Training.strSql <> "" Then
strSql = Training.strSql
Else
strSql = " select top 10 * from VWEmployees where EmpBirthday between '" & cmbBirS.Value.Date & "' And '" & cmbBirE.Value.Date & "' "
End If
ds = GetDataSet(strSql)
If chkChooes.Checked = False Then
xlSheet.Cells(2, 2) = "打印 " & cmbBirS.Value.ToString("yyyy-mm-dd") & " 到 " & cmbBirE.Value.ToString("yyyy-mm-dd")
End If
'循环打印报表内容
For Me.j = 0 To ds.Tables(0).Rows.Count - 1
With xlSheet
.Cells(i, 1).NumberFormatLocal = "@"
.Cells(i, 4).NumberFormatLocal = "@"
.Cells(i, 5).NumberFormatLocal = "@"
.Cells(i, 1) = ds.Tables(0).Rows(j)("EmpID").ToString()
.Cells(i, 2) = ds.Tables(0).Rows(j)("EmpName").ToString()
If ds.Tables(0).Rows(j)("EmpSex").ToString().Equals("1") Then
.Cells(i, 3) = "男"
Else
.Cells(i, 3) = "女"
End If
.Cells(i, 4) = ds.Tables(0).Rows(j)("EmpPID").ToString()
.Cells(i, 5) = ds.Tables(0).Rows(j)("EmpBirthday").ToString()
.Cells(i, 6) = ds.Tables(0).Rows(j)("EmpAge").ToString()
.Cells(i, 7) = ds.Tables(0).Rows(j)("DepID").ToString()
.Cells(i, 8) = ds.Tables(0).Rows(j)("DepName").ToString()
.Cells(i, 9) = ds.Tables(0).Rows(j)("PDepID").ToString()
End With
i = i + 1
Next
'打印报表尾
i = i + 3
xlSheet.Cells(i, 6) = "制表人"
xlSheet.Cells(i, 7) = "XXXXXX"
'对象释放
xlSheet.Application.Visible = True
xlSheet = Nothing
xlBook = Nothing
xlApp = Nothing
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Sub
Private Sub btnGoBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGoBack.Click
Training.Visible = True
Me.Close()
End Sub
Private Sub frmPrint_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
chkChooes.Checked = False
cmbBirS.Value = "1980-01-01"
cmbBirE.Value = "1990-01-01"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Try
Dim y7 As String = "1.00 "
Dim y8 As String = "1.6 "
MsgBox((Convert.ToInt16(y7) * Convert.ToInt16(y8)).ToString())
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class