My program is used to save data read from the Excel file EmployeeMgmt.xlsx. I want to automatically close the form after successfully saving the data to MySQL, delete all lines except the title in the file EmployeeMgmt.xlsx and save the changes. upload this file. Even though I pointed to the correct path of the Excel file, the program created a new file right in the My Documents folder instead of overwriting changes to the original file located in DocumentsSource
Documents
|__….
|__Source
|__EmployeeMgmt.xlsx
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Try
MySqlConn.Open()
Dim savedRowsCount As Integer = 0
For Each row As DataGridViewRow In dgvNew.Rows
If Not row.IsNewRow AndAlso Not String.IsNullOrEmpty(row.Cells("col_ID").Value?.ToString()) Then
Dim employeeID As String = row.Cells("col_ID").Value.ToString()
Query = "SELECT COUNT(*) FROM employee_tb WHERE employee_ID = '" & employeeID & "'"
COMMAND = New MySqlCommand(Query, MySqlConn)
Dim result As Object = COMMAND.ExecuteScalar()
If Convert.ToInt32(result) = 0 Then
Dim employeeName As String = row.Cells("col_Fullname").Value.ToString()
Dim gender As String = row.Cells("col_gender").Value.ToString()
Dim position As String = row.Cells("col_Position").Value.ToString()
Dim department As String = row.Cells("col_Department").Value.ToString()
Dim joinDate As String = row.Cells("col_JoinDate").Value.ToString()
Dim promoteDate As String = row.Cells("col_PromotionDate").Value.ToString()
Dim status As String = row.Cells("col_status").Value.ToString()
Dim queryInsert As String = "INSERT INTO employee_tb (employee_ID, employee_Name, gender, position, department, joinDate, promotionDate, status) VALUES ('" & employeeID & "', '" & employeeName & "', '" & gender & "', '" & position & "', '" & department & "', '" & joinDate & "', '" & promoteDate & "', '" & status & "')"
COMMAND = New MySqlCommand(queryInsert, MySqlConn)
COMMAND.ExecuteNonQuery()
savedRowsCount += 1
End If
End If
Next
MySqlConn.Close()
KryptonMessageBox.Show("Success" & savedRowsCount & , "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Dim ExcelApp As New Excel.Application()
Dim WorkbookPath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Source", "EmployeeMgmt.xlsx")
Dim Wb As Excel.Workbook = ExcelApp.Workbooks.Open(WorkbookPath, Editable:=True)
Dim Ws As Excel.Worksheet = DirectCast(Wb.Worksheets(1), Excel.Worksheet)
Dim rowCount As Integer = Ws.UsedRange.Rows.Count
For i As Integer = rowCount To 2 Step -1
Dim range As Excel.Range = DirectCast(Ws.Rows(i), Excel.Range)
range.Delete()
Next
Wb.Save()
Wb.Close()
ExcelApp.Quit()
Me.Close()
Catch ex As Exception
KryptonMessageBox.Show("Err: " & ex.Message, "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error)
If MySqlConn.State = ConnectionState.Open Then
MySqlConn.Close()
End If
End Try
End Sub
Thanks a lots for your help! ^^
Dim WorkbookPath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Source", "EmployeeMgmt.xlsx")
It should have overwritten the changes to the file following this path
P/s: I use Microsoft.Office.Interop to working with Excel in VB.NET
Dat Nguyen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.