private void DgvGeneral_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e) { if (e.Value != null) { if (e.Value.ToString() != "") { if (e.Column == DgvGeneral.Columns["SerConvi"]) { if (Convert.ToDecimal(e.Value) > 5) { DgvGeneral.SetRowCellValue(e.RowHandle, e.Column, 5); } else { if (Convert.ToDecimal(e.Value) < 0) { DgvGeneral.SetRowCellValue(e.RowHandle, e.Column, Math.Abs(Convert.ToDecimal(e.Value))); } } if (Convert.ToDecimal(e.Value) <= 5 && Convert.ToDecimal(e.Value) >= 0) { decimal nota1 = Decimal.Round(Convert.ToDecimal(e.Value) * Convert.ToDecimal(0.3), 1); decimal nota4 = Decimal.Round(Convert.ToDecimal(DgvGeneral.GetRowCellValue(e.RowHandle, DgvGeneral.Columns["Nota4"])), 1); decimal notaFinal = Decimal.Round(nota1 + nota4, 1); DgvGeneral.SetRowCellValue(e.RowHandle, DgvGeneral.Columns["Nota1"], nota1); DgvGeneral.SetRowCellValue(e.RowHandle, DgvGeneral.Columns["NotaFinal"], notaFinal); } } if (e.Column == DgvGeneral.Columns["Hacer"]) { if (Convert.ToDecimal(e.Value) > 5) { DgvGeneral.SetRowCellValue(e.RowHandle, e.Column, 5); } else { if (Convert.ToDecimal(e.Value) < 0) { DgvGeneral.SetRowCellValue(e.RowHandle, e.Column, Math.Abs(Convert.ToDecimal(e.Value))); } } if (Convert.ToDecimal(e.Value) <= 5 && Convert.ToDecimal(e.Value) >= 0) { decimal nota2 = Decimal.Round(Convert.ToDecimal(e.Value) * Convert.ToDecimal(0.3), 1); decimal nota3 = Decimal.Round(Convert.ToDecimal(DgvGeneral.GetRowCellValue(e.RowHandle, DgvGeneral.Columns["Conocer"])) * Convert.ToDecimal(0.4), 1); decimal saber = Decimal.Round((Convert.ToDecimal(e.Value) + Convert.ToDecimal(DgvGeneral.GetRowCellValue(e.RowHandle, DgvGeneral.Columns["Conocer"]))) / 2, 1); decimal nota4 = Decimal.Round(nota2 + nota3, 1); decimal nota1 = Decimal.Round(Convert.ToDecimal(DgvGeneral.GetRowCellValue(e.RowHandle, DgvGeneral.Columns["Nota1"])), 1); decimal notaFinal = Decimal.Round(nota1 + nota4, 1); DgvGeneral.SetRowCellValue(e.RowHandle, DgvGeneral.Columns["Nota2"], nota2); DgvGeneral.SetRowCellValue(e.RowHandle, DgvGeneral.Columns["Saber"], saber); DgvGeneral.SetRowCellValue(e.RowHandle, DgvGeneral.Columns["Nota4"], nota4); DgvGeneral.SetRowCellValue(e.RowHandle, DgvGeneral.Columns["NotaFinal"], notaFinal); } } if (e.Column == DgvGeneral.Columns["Conocer"]) { if (Convert.ToDecimal(e.Value) > 5) { DgvGeneral.SetRowCellValue(e.RowHandle, e.Column, 5); } else { if (Convert.ToDecimal(e.Value) < 0) { DgvGeneral.SetRowCellValue(e.RowHandle, e.Column, Math.Abs(Convert.ToDecimal(e.Value))); } } if (Convert.ToDecimal(e.Value) <= 5 && Convert.ToDecimal(e.Value) >= 0) { decimal nota2 = Decimal.Round(Convert.ToDecimal(DgvGeneral.GetRowCellValue(e.RowHandle, DgvGeneral.Columns["Hacer"])) * Convert.ToDecimal(0.3), 1); decimal nota3 = Decimal.Round(Convert.ToDecimal(e.Value) * Convert.ToDecimal(0.4), 1); decimal saber = Decimal.Round((Convert.ToDecimal(e.Value) + Convert.ToDecimal(DgvGeneral.GetRowCellValue(e.RowHandle, DgvGeneral.Columns["Hacer"]))) / 2, 1); decimal nota4 = Decimal.Round(nota2 + nota3, 1); decimal nota1 = Decimal.Round(Convert.ToDecimal(DgvGeneral.GetRowCellValue(e.RowHandle, DgvGeneral.Columns["Nota1"])), 1); decimal notaFinal = Decimal.Round(nota1 + nota4, 1); DgvGeneral.SetRowCellValue(e.RowHandle, DgvGeneral.Columns["Nota3"], nota3); DgvGeneral.SetRowCellValue(e.RowHandle, DgvGeneral.Columns["Saber"], saber); DgvGeneral.SetRowCellValue(e.RowHandle, DgvGeneral.Columns["Nota4"], nota4); DgvGeneral.SetRowCellValue(e.RowHandle, DgvGeneral.Columns["NotaFinal"], notaFinal); } } } else { DgvGeneral.SetRowCellValue(e.RowHandle, e.Column, 0); } } else { DgvGeneral.SetRowCellValue(e.RowHandle, e.Column, 0); } }
public void RecogerDatosExcel() { string rutaArchivo = ""; using (var openDialog = new OpenFileDialog()) { openDialog.Filter = "Excel (2003)(.xls)|*.xls|Excel (2010) (.xlsx)|*.xlsx";// if (openDialog.ShowDialog() == DialogResult.OK) { rutaArchivo = openDialog.FileName; } else { return; } } int numHoja = 1; switch (CodPeriodo) { case "02": numHoja = 2; break; case "03": numHoja = 3; break; case "04": numHoja = 4; break; default: numHoja = 1; break; } Excel.Application excelAplicacion; Excel._Workbook libroTrabajo; Excel._Worksheet hojaTrabajo; //Start Excel and get Application object. excelAplicacion = new Excel.Application(); excelAplicacion.Visible = false; try { //Get a new workbook. libroTrabajo = (Excel._Workbook)(excelAplicacion.Workbooks.Open(rutaArchivo, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)); hojaTrabajo = (Excel._Worksheet)libroTrabajo.Sheets[numHoja]; hojaTrabajo.Unprotect("cargape"); string periodoAño = (string)hojaTrabajo.get_Range("G" + 4, Missing.Value).Text; periodoAño = periodoAño.Remove(0, 10); string periodo = (periodoAño.Split('-'))[0].Trim(); string año = (periodoAño.Split('-'))[2].Trim(); if (Año.ToString() != año) { XtraMessageBox.Show("La planilla que intenta importar no corresponde al año electivo (" + Año.ToString() + ").", Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } string curso = (string)hojaTrabajo.get_Range("C" + 6, Missing.Value).Text; curso = curso.Remove(0, 8); string codCurso = (curso.Split('-'))[0].Trim(); if (CodCurso != codCurso) { XtraMessageBox.Show("La planilla que intenta importar no corresponde al curso seleccionado (" + NomCurso + ").", Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } string priAlum = (string)hojaTrabajo.get_Range("A" + 8, Missing.Value).Text; if (string.IsNullOrEmpty(priAlum) || priAlum.Substring(0, 3) != "ALU" || priAlum.Length != 8) { XtraMessageBox.Show("La planilla que intenta importar no posee los códigos de los alumnos.", Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } for (int i = 8; i < 42; i++) { string codAlum = (string)hojaTrabajo.get_Range("A" + i, Missing.Value).Text; if (string.IsNullOrEmpty(codAlum)) { break; } int indice = -1; for (int l = 0; l < DgvGeneral.RowCount; l++) { if (DgvGeneral.GetRowCellDisplayText(l, DgvGeneral.Columns["CodigoAlum"]) == codAlum) { indice = l; break; } } if (indice != -1) { string serConvivir = (string)hojaTrabajo.get_Range("G" + i, Missing.Value).Text; string hacer = (string)hojaTrabajo.get_Range("I" + i, Missing.Value).Text; string conocer = (string)hojaTrabajo.get_Range("K" + i, Missing.Value).Text; string fallas = (string)hojaTrabajo.get_Range("P" + i, Missing.Value).Text; DgvGeneral.SetRowCellValue(indice, DgvGeneral.Columns["SerConvi"], serConvivir); DgvGeneral.SetRowCellValue(indice, DgvGeneral.Columns["Hacer"], hacer); DgvGeneral.SetRowCellValue(indice, DgvGeneral.Columns["Conocer"], conocer); DgvGeneral.SetRowCellValue(indice, DgvGeneral.Columns["Fallas"], fallas); } else { XtraMessageBox.Show("El alumno con codigo (" + codAlum + "), no existe actualmente en el sistema o no se encuentra activo.", Resources.AppName, MessageBoxButtons.OK, MessageBoxIcon.Information); } } //Cerrar el Libro libroTrabajo.Close(false, Missing.Value, Missing.Value); //Cerrar la Aplicación excelAplicacion.Quit(); } catch (Exception exc) { String errorMessage; errorMessage = "Error: "; errorMessage = String.Concat(errorMessage, exc.Message); errorMessage = String.Concat(errorMessage, " Line: "); errorMessage = String.Concat(errorMessage, exc.Source); MessageBox.Show(errorMessage, "Error"); } }