0% found this document useful (0 votes)
255 views

Provider Microsoft - Ace.Oledb.12.0 Data Source - Datadirectory - /payrollmanagerdb - Accdb Persist Security Info False

This code defines an attendance tracking form in Visual Basic for an employee attendance management system. It includes functions for loading employee data into dropdowns, retrieving and saving attendance records, and updating or deleting existing records. The form interacts with an Access database to store and retrieve employee and attendance data.

Uploaded by

RAHUL16398
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
255 views

Provider Microsoft - Ace.Oledb.12.0 Data Source - Datadirectory - /payrollmanagerdb - Accdb Persist Security Info False

This code defines an attendance tracking form in Visual Basic for an employee attendance management system. It includes functions for loading employee data into dropdowns, retrieving and saving attendance records, and updating or deleting existing records. The form interacts with an Access database to store and retrieve employee and attendance data.

Uploaded by

RAHUL16398
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

ublic Class frmAttendance

Dim rdr As oledbDataReader = Nothing


Dim dtable As DataTable
Dim con As oledbConnection = Nothing
Dim adp As oledbDataAdapter
Dim ds As DataSet
Dim cmd As oledbCommand = Nothing
Dim dt As New DataTable
Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|
DataDirectory|\PayrollManagerDB.accdb;Persist Security Info=False;"
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub Attendance_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
frmMainMenu.Show()
End Sub
Sub fillcombo()
Try
Dim CN As New oledbConnection(cs)
CN.Open()
adp = New oledbDataAdapter()
adp.SelectCommand = New oledbCommand("SELECT distinct (employeeid) FROM
employeeregistration", CN)
ds = New DataSet("ds")
adp.Fill(ds)
dtable = ds.Tables(0)
EmployeeID.Items.Clear()
For Each drow As DataRow In dtable.Rows
EmployeeID.Items.Add(drow(0).ToString())
Next
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Attendance_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
fillcombo()
DataGridView1.DataSource = GetData()
End Sub
Private Sub EmployeeID_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles EmployeeID.SelectedIndexChanged
Try
Delete.Enabled = True
con = New oledbConnection(cs)
con.Open()

Dim ct As String = "select employeename,basicworkingtime from employeeregistration


where employeeid=@find"
cmd = New oledbCommand(ct)
cmd.Connection = con
cmd.Parameters.Add(New oledbParameter("@find", oledbType.Varchar, 30,
"employeeid"))
cmd.Parameters("@find").Value = Trim(EmployeeID.Text)
rdr = cmd.ExecuteReader()
If rdr.Read Then
EmployeeName.Text = Trim(rdr.GetString(0))
BasicWorkingTime.Text = Trim(rdr.GetString(1))
End If
If Not rdr Is Nothing Then
rdr.Close()
End If
If con.State = ConnectionState.Open Then
con.Close()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Sub Reset()
WorkingDate.Text = Today
EmployeeID.Text = ""
EmployeeName.Text = ""
InTime.Text = Now
OutTime.Text = Now
Overtime.Text = ""
Status.Text = ""
BasicWorkingTime.Text = ""
Delete.Enabled = False
Update_Record.Enabled = False
txtOutTime.Visible = False
txtInTime.Visible = False
Save.Enabled = True
End Sub
Private Sub NewRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles NewRecord.Click
Reset()
End Sub
Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Save.Click
If Len(Trim(EmployeeID.Text)) = 0 Then
MessageBox.Show("Please select employee id", "Input Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
EmployeeID.Focus()
Exit Sub
End If
If Len(Trim(Status.Text)) = 0 Then

MessageBox.Show("Please select Status", "Input Error", MessageBoxButtons.OK,


MessageBoxIcon.Error)
Status.Focus()
Exit Sub
End If
Try
con = New OleDbConnection(cs)
con.Open()
Dim ct As String = "select employeeid,workingdate from employeeattendance where
employeeid='" & EmployeeID.Text & "' and workingdate= #" & WorkingDate.Text & "#"
cmd = New OleDbCommand(ct)
cmd.Connection = con
rdr = cmd.ExecuteReader()
If rdr.Read Then
MessageBox.Show("Employee today's attendance is already saved", "Input Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
EmployeeID.Focus()
If Not rdr Is Nothing Then
rdr.Close()
End If
Else
con = New OleDbConnection(cs)
con.Open()
Dim cb As String = "insert into
employeeAttendance(Workingdate,employeeid,status,intime,outtime,overtime,basicworkingtime)
VALUES (@d1,@d2,@d4,@d5,@d6,@d7,@d8)"
cmd = New OleDbCommand(cb)
cmd.Connection = con
cmd.Parameters.Add(New
"Workingdate"))
cmd.Parameters.Add(New
"employeeid"))
cmd.Parameters.Add(New
cmd.Parameters.Add(New
cmd.Parameters.Add(New
cmd.Parameters.Add(New
"overtime"))
cmd.Parameters.Add(New
"basicworkingtime"))

OleDbParameter("@d1", OleDbType.VarChar, 30,


OleDbParameter("@d2", OleDbType.VarChar, 30,
OleDbParameter("@d4",
OleDbParameter("@d5",
OleDbParameter("@d6",
OleDbParameter("@d7",

OleDbType.VarChar,
OleDbType.VarChar,
OleDbType.VarChar,
OleDbType.VarChar,

10, "status"))
30, "intime"))
30, "outtime"))
10,

OleDbParameter("@d8", OleDbType.VarChar, 10,

cmd.Parameters("@d1").Value = WorkingDate.Text
cmd.Parameters("@d2").Value = EmployeeID.Text
cmd.Parameters("@d4").Value = Status.Text
If Status.Text = "P" Then

cmd.Parameters("@d5").Value
cmd.Parameters("@d6").Value
ElseIf Status.Text = "A" Then
cmd.Parameters("@d5").Value
cmd.Parameters("@d6").Value
End If

= InTime.Text
= OutTime.Text
= txtInTime.Text
= txtOutTime.Text

cmd.Parameters("@d7").Value = Overtime.Text
cmd.Parameters("@d8").Value = BasicWorkingTime.Text
cmd.ExecuteReader()
MessageBox.Show("Successfully saved", "Employee Attendance",
MessageBoxButtons.OK, MessageBoxIcon.Information)
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Close()
Save.Enabled = False
End If

Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Submit.Click
Try
If EmployeeID.Text = "" Then
MessageBox.Show("Please select employee id", "input error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
Exit Sub
End If
Update_Record.Enabled = True
Delete.Enabled = True
con = New oledbConnection(cs)
con.Open()
Dim ct As String = "select
Employeename,EmployeeAttendance.Basicworkingtime,status,intime,outtime,overtime from
EmployeeAttendance,EmployeeRegistration where
EmployeeRegistration.EmployeeID=EmployeeAttendance.EmployeeID and
EmployeeRegistration.employeeid=@find and WorkingDate=@find1"
cmd = New oledbCommand(ct)
cmd.Connection = con
cmd.Parameters.Add(New oledbParameter("@find", oledbType.Varchar, 30,
"employeeid"))
cmd.Parameters("@find").Value = Trim(EmployeeID.Text)
cmd.Parameters.Add(New oledbParameter("@find1", oledbType.Varchar, 30,
"workingdate"))

cmd.Parameters("@find1").Value = Trim(WorkingDate.Text)
rdr = cmd.ExecuteReader()
If rdr.Read Then
EmployeeName.Text = Trim(rdr.GetString(0))
BasicWorkingTime.Text = Trim(rdr.GetString(1))
Status.Text = Trim(rdr.GetString(2))
If Status.Text = "P" Then
InTime.Text = Trim(rdr.GetString(3))
OutTime.Text = Trim(rdr.GetString(4))
ElseIf Status.Text = "A" Then
txtOutTime.Visible = True
txtInTime.Visible = True
txtOutTime.Text = Trim(rdr.GetString(3))
txtInTime.Text = Trim(rdr.GetString(4))
End If
InTime.Text = Trim(rdr.GetString(3))
OutTime.Text = Trim(rdr.GetString(4))
Overtime.Text = Trim(rdr.GetString(5))
Else
MessageBox.Show("No record found", "Sorry", MessageBoxButtons.OK,
MessageBoxIcon.Information)
End If
If Not rdr Is Nothing Then
rdr.Close()
End If
If con.State = ConnectionState.Open Then
con.Close()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Update_Record_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Update_Record.Click
Try
con = New OleDbConnection(cs)
con.Open()
Dim cb As String = "update employeeAttendance set status='" & Status.Text &
"',intime=@d5,outtime=@d6,overtime='" & Overtime.Text & "',basicworkingtime='" &
BasicWorkingTime.Text & "' where workingdate= #" & WorkingDate.Text & "# and Employeeid='"
& EmployeeID.Text & "'"
cmd = New oledbCommand(cb)
cmd.Connection = con
cmd.Parameters.Add(New OleDbParameter("@d5", OleDbType.VarChar, 30, "intime"))
cmd.Parameters.Add(New oledbParameter("@d6", oledbType.Varchar, 30, "outtime"))
If Status.Text = "P" Then

cmd.Parameters("@d5").Value
cmd.Parameters("@d6").Value
ElseIf Status.Text = "A" Then
cmd.Parameters("@d5").Value
cmd.Parameters("@d6").Value
End If

= InTime.Text
= OutTime.Text
= txtOutTime.Text
= txtInTime.Text

If MessageBox.Show("Are you sure want to update the record?", "Confirmation",


MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
cmd.ExecuteReader()
MessageBox.Show("Successfully updated", "Record", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Reset()
End If
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Private Sub Delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Delete.Click
Try
If EmployeeID.Text = "" Then
MessageBox.Show("Please select employee id", "Entry", MessageBoxButtons.OK,
MessageBoxIcon.Warning)
Exit Sub
End If
If EmployeeID.Items.Count > 0 Then
If MessageBox.Show("Do you really want to delete the records?" & vbCrLf & "You can
not restore the records" & vbCrLf & "It will delete all the attendance records permanently" &
vbCrLf & "related to selected employee", "Employee's Attendance", MessageBoxButtons.YesNo,
MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
delete_records()
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub delete_records()
Try
Dim RowsAffected As Integer = 0
con = New oledbConnection(cs)
con.Open()
Dim cq As String = "delete from employeeattendance where EmployeeID=@DELETE1;"

cmd = New OleDbCommand(cq)


cmd.Connection = con
cmd.Parameters.Add(New oledbParameter("@DELETE1", oledbType.Varchar, 30,
"EmployeeID"))
cmd.Parameters("@DELETE1").Value = Trim(EmployeeID.Text)
RowsAffected = cmd.ExecuteNonQuery()
If RowsAffected > 0 Then
MessageBox.Show("Successfully deleted", "Record", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Reset()
Else
MessageBox.Show("No record found", "Sorry", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Reset()
con.Close()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private ReadOnly Property Connection() As OleDbConnection
Get
Dim ConnectionToFetch As New OleDbConnection(cs)
ConnectionToFetch.Open()
Return ConnectionToFetch
End Get
End Property
Public Function GetData() As DataView
Dim SelectQry = "SELECT (EmployeeName) as [Employee Name],(EmployeeID) as
[Employee ID] FROM EmployeeRegistration order by employeename"
Dim SampleSource As New DataSet
Dim TableView As DataView
Try
Dim SampleCommand As New oledbCommand()
Dim SampleDataAdapter = New oledbDataAdapter()
SampleCommand.CommandText = SelectQry
SampleCommand.Connection = Connection
SampleDataAdapter.SelectCommand = SampleCommand
SampleDataAdapter.Fill(SampleSource)
TableView = SampleSource.Tables(0).DefaultView
Catch ex As Exception
Throw ex
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Return TableView
End Function
Private Sub Overtime_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles Overtime.KeyPress
If (e.KeyChar < Chr(48) Or e.KeyChar > Chr(57)) And e.KeyChar <> Chr(8) Then
e.Handled = True

End If
End Sub
Private Sub Status_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Status.SelectedIndexChanged
If Status.Text = "P" Then
txtOutTime.Visible = False
txtInTime.Visible = False
InTime.Enabled = True
OutTime.Enabled = True
InTime.Text = Now
OutTime.Text = Now
Overtime.Text = ""
ElseIf Status.Text = "A" Then
txtOutTime.Visible = True
txtInTime.Visible = True
txtOutTime.Text = "00:00:00"
txtInTime.Text = "00:00:00"
Overtime.Text = "00:00:00"
End If
End Sub
Private Sub OutTime_ValueChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles OutTime.ValueChanged
Dim ts As TimeSpan
TimeSpan.TryParse(BasicWorkingTime.Text, ts)
Dim duration As TimeSpan = OutTime.Value - InTime.Value
Overtime.Text = Convert.ToString(duration - ts)
End Sub
Private Sub InTime_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Handles InTime.ValueChanged
Dim ts As TimeSpan
TimeSpan.TryParse(BasicWorkingTime.Text, ts)
Dim duration As TimeSpan = OutTime.Value - InTime.Value
Overtime.Text = Convert.ToString(duration - ts)
End Sub
Private Sub DataGridView1_RowHeaderMouseClick(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles
DataGridView1.RowHeaderMouseClick
Try
Dim dr As DataGridViewRow = DataGridView1.SelectedRows(0)
Me.EmployeeName.Text = dr.Cells(0).Value.ToString()
Me.EmployeeID.Text = dr.Cells(1).Value.ToString()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Label2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Label2.Click
End Sub
End Class
Imports System.Data.OleDb
Imports Excel = Microsoft.Office.Interop.Excel
Public Class frmAdvanceEntryRecord

Dim rdr As oledbDataReader = Nothing


Dim dtable As DataTable
Dim con As oledbConnection = Nothing
Dim adp As oledbDataAdapter
Dim ds As DataSet
Dim cmd As oledbCommand = Nothing
Dim dt As New DataTable
Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|
DataDirectory|\PayrollManagerDB.accdb;Persist Security Info=False;"
Private Sub AdvanceEntryRecord_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Me.Hide()
frmMainMenu.Show()
End Sub
Sub fillcombo()
Try
Dim CN As New oledbConnection(cs)
CN.Open()
adp = New oledbDataAdapter()
adp.SelectCommand = New OleDbCommand("SELECT distinct (employeename) FROM
Advanceentry,employeeRegistration where
EmployeeRegistration.EmployeeID=AdvanceEntry.EmployeeID", CN)
ds = New DataSet("ds")
adp.Fill(ds)
dtable = ds.Tables(0)
EmployeeName.Items.Clear()
For Each drow As DataRow In dtable.Rows
EmployeeName.Items.Add(drow(0).ToString())
Next
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub AdvanceEntryRecord_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
fillcombo()
End Sub
Private Sub EmployeeName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles EmployeeName.SelectedIndexChanged
Try
Total.Visible = True
con = New OleDbConnection(cs)
con.Open()
cmd = New OleDbCommand("select (workingdate) as [Entry Date],
(EmployeeRegistration.EmployeeID) as [Employee ID],(EmployeeName) as [EmployeeName],
(Amount) as [Advance] from Advanceentry,employeeRegistration where
EmployeeRegistration.EmployeeID=AdvanceEntry.EmployeeID and amount > 0 and
Employeename = '" & EmployeeName.Text & "'order by workingdate", con)
Dim myDA As oledbDataAdapter = New oledbDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "AdvanceEntry")
myDA.Fill(myDataSet, "EmployeeRegistration")
DataGridView1.DataSource = myDataSet.Tables("AdvanceEntry").DefaultView

DataGridView1.DataSource = myDataSet.Tables("EmployeeRegistration").DefaultView
Dim sum As Double = 0
For Each r As DataGridViewRow In Me.DataGridView1.Rows
sum = sum + r.Cells(3).Value
Next
TotalAdvance.Text = sum
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles button8.Click
DataGridView1.DataSource = Nothing
EmployeeName.Text = ""
Total.Visible = False
End Sub
Private Sub button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles button4.Click
Try
GroupBox1.Visible = True
con = New OleDbConnection(cs)
con.Open()
cmd = New OleDbCommand("select (workingdate) as [Entry Date],
(EmployeeRegistration.EmployeeID) as [Employee ID],(EmployeeName) as [EmployeeName],
(Amount) as [Advance] from Advanceentry,employeeRegistration where
EmployeeRegistration.EmployeeID=AdvanceEntry.EmployeeID and amount > 0 and WorkingDate
between #" & DateFrom.Text & "# And #" & DateTo.Text & "# order by workingdate", con)
Dim myDA As oledbDataAdapter = New oledbDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "AdvanceEntry")
myDA.Fill(myDataSet, "EmployeeRegistration")
DataGridView2.DataSource = myDataSet.Tables("AdvanceEntry").DefaultView
DataGridView2.DataSource = myDataSet.Tables("EmployeeRegistration").DefaultView
Dim sum As Double = 0
For Each r As DataGridViewRow In Me.DataGridView2.Rows
sum = sum + r.Cells(3).Value
Next
TextBox1.Text = sum
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles button5.Click
DateFrom.value = Today
DateTo.value = Today
DataGridView2.DataSource = Nothing
GroupBox1.Visible = False
End Sub

Private Sub TabControl1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles


TabControl1.Click
DateFrom.value = Today
DateTo.value = Today
DataGridView2.DataSource = Nothing
GroupBox1.Visible = False
DataGridView1.DataSource = Nothing
EmployeeName.Text = ""
Total.Visible = False
End Sub
Private Sub button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles button6.Click
If DataGridView1.RowCount = Nothing Then
MessageBox.Show("Sorry nothing to export into excel sheet.." & vbCrLf & "Please retrieve
data in datagridview", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
Dim rowsTotal, colsTotal As Short
Dim I, j, iC As Short
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
Dim xlApp As New Excel.Application
Try
Dim excelBook As Excel.Workbook = xlApp.Workbooks.Add
Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1),
Excel.Worksheet)
xlApp.Visible = True
rowsTotal = DataGridView1.RowCount - 1
colsTotal = DataGridView1.Columns.Count - 1
With excelWorksheet
.Cells.Select()
.Cells.Delete()
For iC = 0 To colsTotal
.Cells(1, iC + 1).Value = DataGridView1.Columns(iC).HeaderText
Next
For I = 0 To rowsTotal - 1
For j = 0 To colsTotal
.Cells(I + 2, j + 1).value = DataGridView1.Rows(I).Cells(j).Value
Next j
Next I
.Rows("1:1").Font.FontStyle = "Bold"
.Rows("1:1").Font.Size = 12
.Cells.Columns.AutoFit()
.Cells.Select()
.Cells.EntireColumn.AutoFit()
.Cells(1, 1).Select()
End With
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
'RELEASE ALLOACTED RESOURCES
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
xlApp = Nothing
End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button1.Click
If DataGridView2.RowCount = Nothing Then
MessageBox.Show("Sorry nothing to export into excel sheet.." & vbCrLf & "Please retrieve
data in datagridview", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
Dim rowsTotal, colsTotal As Short
Dim I, j, iC As Short
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
Dim xlApp As New Excel.Application
Try
Dim excelBook As Excel.Workbook = xlApp.Workbooks.Add
Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1),
Excel.Worksheet)
xlApp.Visible = True
rowsTotal = DataGridView2.RowCount - 1
colsTotal = DataGridView2.Columns.Count - 1
With excelWorksheet
.Cells.Select()
.Cells.Delete()
For iC = 0 To colsTotal
.Cells(1, iC + 1).Value = DataGridView2.Columns(iC).HeaderText
Next
For I = 0 To rowsTotal - 1
For j = 0 To colsTotal
.Cells(I + 2, j + 1).value = DataGridView2.Rows(I).Cells(j).Value
Next j
Next I
.Rows("1:1").Font.FontStyle = "Bold"
.Rows("1:1").Font.Size = 12
.Cells.Columns.AutoFit()
.Cells.Select()
.Cells.EntireColumn.AutoFit()
.Cells(1, 1).Select()
End With
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
'RELEASE ALLOACTED RESOURCES
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
xlApp = Nothing
End Try
End Sub
End Class

You might also like