public static IList <AutoahorroDato> ParseoOfertasTXT(Stream input, ref ArchivoAutoahorro archivoAutoahorro) { using (StreamReader streamReader = new StreamReader(input)) { string archivo = streamReader.ReadToEnd(); string pattern = @"Acto.+(?<Acto>\d{3}).+Fecha (?<Fecha>\d{2}/\d{2}/\d{2}).+Concesionario:(?<Concesionario>.+)\n.+Ofertas Recibidas (?<ofertasRecibidas>\d{2}/\d{2})"; foreach (Match m in Regex.Matches(archivo, pattern)) { archivoAutoahorro.Acto = int.Parse(m.Groups["Acto"].Value.Trim()); archivoAutoahorro.Fecha = DateTime.ParseExact(m.Groups["Fecha"].Value.Trim(), "dd/MM/yy", null); archivoAutoahorro.Concesionario = m.Groups["Concesionario"].Value.Trim(); archivoAutoahorro.OfertasRecibidas = DateTime.ParseExact(m.Groups["ofertasRecibidas"].Value.Trim(), "MM/yy", null); break; } string[] patterns = new string[3]; patterns[0] = @"[ ]*(?<SECNRO>\d{1,4})[ ]*(?<GRUPO>\d{4})-(?<ORDEN>\d{3}).*"; patterns[1] = @"[ ]*(?<MODELO>[A-Za-z0-9]{5})[ ]*(?<TAJUSTADO>[0-9]+\.[0-9]{2})[ ]*(?<TLICITADO>[0-9]+\.[0-9]{2})(?<OBSERVACION>.{10})[ ]*(?<CONCESIONARIO>\d{0,5})"; int patternActual = 0; int i = 0; List <AutoahorroDato> res = new List <AutoahorroDato>(); AutoahorroOferta oferta = null; string linea; bool flag1, flag2; flag1 = flag2 = false; streamReader.BaseStream.Seek(0, SeekOrigin.Begin); while ((linea = streamReader.ReadLine()) != null) { Match matchPattern = Regex.Match(linea, patterns[patternActual]); if (patternActual == 0 && matchPattern.Success) { patternActual = 1; oferta = new AutoahorroOferta(); oferta.SecNro = matchPattern.Groups["SECNRO"].Value.Trim(); oferta.Grupo = matchPattern.Groups["GRUPO"].Value.Trim(); oferta.Orden = matchPattern.Groups["ORDEN"].Value.Trim(); flag1 = true; } else if (patternActual == 1 && matchPattern.Success) { patternActual = 2; if (oferta != null) { patternActual = 0; oferta.Modelo = matchPattern.Groups["MODELO"].Value.Trim(); oferta.TAjustado = float.Parse(matchPattern.Groups["TAJUSTADO"].Value, CultureInfo.InvariantCulture); oferta.TLicitado = float.Parse(matchPattern.Groups["TLICITADO"].Value, CultureInfo.InvariantCulture); oferta.Observacion = matchPattern.Groups["OBSERVACION"].Value.Trim(); oferta.Concesionario = matchPattern.Groups["CONCESIONARIO"].Value.Trim(); } flag2 = true; } if (flag1 && flag2) { //Console.WriteLine(string.Format("gpo = {0}\nord = {1}\ndc = {2}\ndesv = {3}\ncuot = {4}\ndc2 = {5}\nplan = {6}\nmod = {7}\nvence = {8}\nnombre = {9}\nbanco = {10}\nsucursal = {11}\ncuenta = {12}\nalicuota = {13}\ncargos = {14}\nactalicuota = {15}\ncaactalic = {16}\nsegvida = {17}\nsegbien = {18}\nmora = {19}\ndebcred = {20}\nintliquid = {21}\notros = {22}\ntotal = {23}\n", gpo, ord, dc, desv, cuot, dc2, plan, mod, vence, nombre, banco, sucursal, cuenta, alicuota, cargos, actalicuota, caactalic, segvida, segbien, mora, debcred, intliquid, otros, total)); res.Add(oferta); flag1 = flag2 = false; i++; } } return(res); } }
public static IList <AutoahorroDato> ParseoOfertasXLS(Stream input, ref ArchivoAutoahorro archivoAutoahorro) { HSSFWorkbook hssfwb; hssfwb = new HSSFWorkbook(input); ISheet sheet = hssfwb.GetSheetAt(0); if (sheet.LastRowNum > 3) { //ACTO N° 460 - REALIZADO EL 11-3-2016 CONCESIONARIO: AUTOTAG S.A. string pattern = @"ACTO.+(?<Acto>\d{3}).+REALIZADO EL (?<Fecha>\d{1,2}-\d{1,2}-\d{4}).+CONCESIONARIO:(?<Concesionario>.+).+"; Regex regEx = new Regex(pattern, RegexOptions.IgnoreCase); Match m = regEx.Match(sheet.GetRow(0).GetCell(0).StringCellValue); while (m.Success) { archivoAutoahorro.Acto = int.Parse(m.Groups["Acto"].Value.Trim()); archivoAutoahorro.Fecha = DateTime.ParseExact(m.Groups["Fecha"].Value.Trim(), new string[] { "dd-M-yyyy", "dd-MM-yyyy", "d-M-yyyy", "d-MM-yyyy" }, CultureInfo.InvariantCulture, DateTimeStyles.None); archivoAutoahorro.Concesionario = m.Groups["Concesionario"].Value.Trim(); m = m.NextMatch(); } pattern = @".+ASAMBLEA (?<ofertasRecibidas>\d{1,2}/\d{4})"; regEx = new Regex(pattern, RegexOptions.IgnoreCase); m = regEx.Match(sheet.GetRow(1).GetCell(0).StringCellValue); while (m.Success) { archivoAutoahorro.OfertasRecibidas = DateTime.ParseExact(m.Groups["ofertasRecibidas"].Value.Trim(), new string [] { "MM/yyyy", "M/yyyy" }, CultureInfo.InvariantCulture, DateTimeStyles.None); m = m.NextMatch(); } List <AutoahorroDato> ofertas = new List <AutoahorroDato>(); AutoahorroOferta oferta = null; for (int row = 3; row <= sheet.LastRowNum; row++) { if (sheet.GetRow(row) != null) //null is when the row only contains empty cells { if (Regex.IsMatch(sheet.GetRow(row).GetCell(0).StringCellValue, "^\\d+$")) { oferta = new AutoahorroOferta(); oferta.SecNro = (sheet.GetRow(row).GetCell(0) != null) ? sheet.GetRow(row).GetCell(0).StringCellValue.Trim() : string.Empty; oferta.Grupo = (sheet.GetRow(row).GetCell(1) != null) ? sheet.GetRow(row).GetCell(1).StringCellValue.Trim() : string.Empty; oferta.Orden = (sheet.GetRow(row).GetCell(2) != null) ? sheet.GetRow(row).GetCell(2).StringCellValue.Trim() : string.Empty; oferta.Modelo = (sheet.GetRow(row).GetCell(3) != null) ? sheet.GetRow(row).GetCell(3).StringCellValue.Trim() : string.Empty; oferta.TAjustado = (sheet.GetRow(row).GetCell(4) != null) ? float.Parse(sheet.GetRow(row).GetCell(4).StringCellValue.Trim(), CultureInfo.InvariantCulture) : 0; oferta.TLicitado = (sheet.GetRow(row).GetCell(5) != null) ? float.Parse(sheet.GetRow(row).GetCell(5).StringCellValue.Trim(), CultureInfo.InvariantCulture) : 0; oferta.Observacion = (sheet.GetRow(row).GetCell(6) != null) ? (sheet.GetRow(row).GetCell(6).CellType == CellType.Numeric) ? sheet.GetRow(row).GetCell(6).NumericCellValue.ToString().Trim() : sheet.GetRow(row).GetCell(6).StringCellValue.Trim() : string.Empty; oferta.Concesionario = (sheet.GetRow(row).GetCell(7) != null) ? sheet.GetRow(row).GetCell(7).StringCellValue.Trim() : string.Empty; ofertas.Add(oferta); } } } return(ofertas); } return(null); }