public static void WriteToExcel(Votante emp) { try { bool existe = false; for (int index = 2; index <= lastRow; index++) { System.Array MyValues = (System.Array)MySheet.get_Range("A" + index.ToString(), "E" + index.ToString()).Cells.Value; string cedula = MyValues.GetValue(1, 3).ToString(); if (emp.Cedula_ID == cedula) { existe = true; } } if (existe) { MessageBox.Show("La cedula ingresada ya Existe", "Alerta!!..", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { lastRow += 1; MySheet.Cells[lastRow, 1] = emp.Nombres; MySheet.Cells[lastRow, 2] = emp.Apellidos; MySheet.Cells[lastRow, 3] = emp.Cedula_ID; MySheet.Cells[lastRow, 4] = emp.Candidato; MySheet.Cells[lastRow, 5] = DateTime.Now; EmpList.Add(emp); MyBook.Save(); MessageBox.Show("Details were successfully added to the excel !!", "Success..", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { } }
private void button1_Click(object sender, EventArgs e) { Votante emp = new Votante { Nombres = textName.Text.ToString(), Cedula_ID = textCedulaID.Text.ToString(), Apellidos = txtApellidos.Text.ToString(), Candidato = cbxCandidato.SelectedItem.ToString() }; MyExcel.WriteToExcel(emp); clearAllFields(); textName.Focus(); }