public IEnumerable <ListResult> LeerExcel(string archivo, bool archivook, string descripcion) { try { Application xlApp; Workbook xlWorkBook; Worksheet xlWorkSheet; Range range; var misValue = Type.Missing;//System.Reflection.Missing.Value; // abrir el documento xlApp = new Application(); xlWorkBook = xlApp.Workbooks.Open(archivo, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue); // seleccion de la hoja de calculo // get_item() devuelve object y numera las hojas a partir de 1 xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1); // seleccion rango activo range = xlWorkSheet.UsedRange; // leer las celdas int rows = range.Rows.Count; int cols = range.Columns.Count; string RowEmbosado, RowNombre, RowNomina; RowEmbosado = (range.Cells[3, 2] as Range).Value2.ToString(); RowNombre = (range.Cells[4, 2] as Range).Value2.ToString(); RowNomina = (range.Cells[5, 2] as Range).Value2.ToString(); bool iniciaRow = false, movOk = false; //CultureInfo ci = new CultureInfo("es-MX"); List <ListExcelResult> RowsExcel = new List <ListExcelResult>(); for (int row = 2; row <= rows; row++) { string RowTarjeta = ""; //try //{ RowTarjeta = (range.Cells[row, 1] as Range).Value2.ToString().Replace("'", ""); if (iniciaRow == true && RowTarjeta != "") { var ImporteOk = (range.Cells[row, 5] as Range).Value2; if (ImporteOk != null) { var RowImporte = (range.Cells[row, 5] as Range).Value2.ToString(); RowImporte = Convert.ToDecimal(RowImporte); if (RowImporte > 0) { string RowDescripcion = (range.Cells[row, 4] as Range).Value2.ToString(); if (RowDescripcion.Trim() != "MOV.REVERSION RECARGA EFECTIVO" && RowDescripcion.Trim() != "") { var RowTipo = (range.Cells[row, 2] as Range).Value2.ToString(); string FechaExcel = (range.Cells[row, 3] as Range).Value2.ToString(); double date = double.Parse(FechaExcel); string RowFecha = DateTime.FromOADate(date).AddDays(-1).ToString("dd/MM/yyyy"); ListExcelResult ColsExcel = new ListExcelResult { Tarjeta = RowTarjeta, Tipo = RowTipo, Fecha = RowFecha, Descripcion = RowDescripcion, Importe = RowImporte }; RowsExcel.Add(ColsExcel); movOk = true; } } } } else if (RowTarjeta.Trim() == "TARJETA") { iniciaRow = true; } //} //catch (Exception err) //{ // string error = err.ToString(); //} } // cerrar xlWorkBook.Close(false, misValue, misValue); xlApp.Quit(); // liberar releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); bool ArcOk = movOk == true ? archivook : movOk; ListResult valores = new ListResult { ArchivoOk = ArcOk, Descripcion = descripcion, Embosado = RowEmbosado, Nombre = RowNombre, Nomina = RowNomina, RowExcel = RowsExcel }; List <ListResult> respuesta = new List <ListResult> { valores }; DeleteArchivo(archivo);//eliminar el archivo return(respuesta); } catch (Exception err) { ListResult valores = new ListResult { ArchivoOk = false, Descripcion = err.ToString(), Embosado = "", Nombre = "", Nomina = "", RowExcel = null }; List <ListResult> respuesta = new List <ListResult> { valores }; return(respuesta); } }
public IEnumerable <ListResult> LeerExcel(string archivo, bool archivook, string descripcion) { //Save the uploaded Excel file. string filePath = archivo; List <ListResult> respuesta = null; //Open the Excel file using ClosedXML. using (XLWorkbook workBook = new XLWorkbook(filePath)) { //Read the first Sheet from Excel file. IXLWorksheet workSheet = workBook.Worksheet(1); //Loop through the Worksheet rows. int i = 1; bool movOk = false; string RowEmbosado = "", RowNombre = "", RowNomina = ""; List <ListExcelResult> RowsExcel = new List <ListExcelResult>(); foreach (IXLRow row in workSheet.Rows()) { /* * if (i == 3 && RowEmbosado == "") * RowEmbosado = row.Cell("B").GetString(); * if (i == 4 && RowNombre == "") * RowNombre = row.Cell("B").GetString(); * if (i == 5 && RowNomina == "") * RowNomina = row.Cell("B").GetString(); */ if (i > 1) { try { string RowTarjeta = ""; RowTarjeta = row.Cell("A").GetString().Replace("'", ""); if (RowTarjeta != "") { var ImporteOk = row.Cell("E").Value.ToString(); if (ImporteOk != "") { double RowImporte = Convert.ToDouble(ImporteOk); if (RowImporte > 0) { string RowDescripcion = row.Cell("D").GetString(); if ((RowDescripcion.Trim() != "MOV.REVERSION RECARGA EFECTIVO" && RowDescripcion.Trim() != "DECREMENTO ON LINE" && RowDescripcion.Trim() != "DECREMENTO ONLINE") && RowDescripcion.Trim() != "") { string RowTipo = row.Cell("B").GetString(); string RowFecha = row.Cell("C").GetString();//.ToString("dd/MM/yyyy"); ListExcelResult ColsExcel = new ListExcelResult { Tarjeta = RowTarjeta, Tipo = RowTipo, Fecha = RowFecha, Descripcion = RowDescripcion, Importe = RowImporte }; RowsExcel.Add(ColsExcel); movOk = true; } } } } } catch (Exception err) { ListExcelResult ColsExcel = new ListExcelResult { Tarjeta = "Error", Tipo = "Row", Fecha = null, Descripcion = err.ToString(), Importe = 0 }; RowsExcel.Add(ColsExcel); } } i++; } bool ArcOk = movOk == true ? archivook : movOk; ListResult valores = new ListResult { ArchivoOk = ArcOk, Descripcion = descripcion, Embosado = RowEmbosado, Nombre = RowNombre, Nomina = RowNomina, RowExcel = RowsExcel }; respuesta = new List <ListResult> { valores }; } File.Delete(filePath); return(respuesta); }