private ResultadoOperacion GetExcel() { Excel = Path.Combine(PathColeccion, Path.GetFileName(PathColeccion) + ".xls"); if (!File.Exists(Excel)) { Excel = Path.Combine(PathColeccion, Path.GetFileName(PathColeccion) + ".xlsx"); if (!File.Exists(Excel)) { { return(ResultadoOperacion.Fail("No se encontro el archivo excel de la coleccion: " + Path.GetDirectoryName(PathColeccion) + ".xls")); } } } return(new ResultadoOperacion { Ok = true }); }
public ResultadoOperacion GetFile(ItemData itemData) { if (itemData.NroCd == 0 || itemData.NroPista == 0) { return(ResultadoOperacion.Fail("CdPista Invalido: " + itemData)); } string colFolder = null; foreach (var pattern in FolderPatterns) { var searchPattern = GetPattern(pattern, itemData); colFolder = Directory.GetDirectories(Path.Combine(PathMusicas, itemData.Coleccion), searchPattern).FirstOrDefault(); if (colFolder != null) { break; } } if (colFolder == null) { return(ResultadoOperacion.Fail("Carpeta no encontrada. " + itemData)); } string[] files = { }; var msg = ""; foreach (var pattern in FilePatterns) { var searchPattern = GetPattern(pattern, itemData); files = Directory.GetFiles(colFolder, searchPattern); if (files.Length == 0) { continue; } if (files.Length == 1) { itemData.Archivo = Path.GetFileName(files[0]); itemData.Carpeta = Path.GetDirectoryName(files[0].Substring(PathMusicas.Length + 1)); return(new ResultadoOperacion { Mensaje = files[0].Substring(PathMusicas.Length) }); } if (string.IsNullOrWhiteSpace(msg)) { msg = $"busqueda: {searchPattern}. Hay {files.Length} archivos y debe haber 1 solo. {itemData}.: " + Environment.NewLine; foreach (var file in files) { msg += " " + file + Environment.NewLine; } } } if (files.Length == 0 && string.IsNullOrWhiteSpace(msg)) { msg = "Archivo de música no encontrado. " + itemData; } return(ResultadoOperacion.Fail(msg)); //if (!pista.StartsWith("0")) return null; //files = Directory.GetFiles(colFolder, pista.Substring(1) + "*"); //if (files.Length == 1) return files[0].Substring(root.Length); //if (files.Length <= 1) return null; //int i; //var file = files.FirstOrDefault(x => !int.TryParse(x.Substring(1, 1), out i)); //return file.Substring(root.Length); }
public ResultadoOperacion HyperLinks() { var filePatterns = File.ReadAllLines("FormatosArchivosMusica.txt"); var folderPatterns = File.ReadAllLines("FormatosCarpetas.txt"); var searchFile = new FileSearch() { FilePatterns = filePatterns, FolderPatterns = folderPatterns, PathMusicas = PathColeccion }; var ro = GetExcel(); if (!ro.Ok) { return(ro); } try { using (var fs = File.OpenRead(Excel)) { hssfWorkbook = new HSSFWorkbook(fs); } } catch (Exception e) { return(ResultadoOperacion.Fail($"No se pudo abrir el excel {Excel}. Error: {e.Message}.")); } var sheet = hssfWorkbook.GetSheet(Hoja); if (sheet == null) { return(ResultadoOperacion.Fail($"La hoja {Hoja} no existe en el archivo {Excel}")); } var row = 1; var linkStyle = hssfWorkbook.CreateCellStyle(); linkStyle.Alignment = HorizontalAlignment.Center; var fontWindings3 = hssfWorkbook.CreateFont(); fontWindings3.FontName = "Wingdings 3"; fontWindings3.FontHeightInPoints = 16; linkStyle.SetFont(fontWindings3); var filas = 0; var filasError = 0; while (true) { var xlRow = sheet.GetRow(row); if (xlRow == null) { break; } filas++; var cell = xlRow.GetCell(ColumnCdPista); row++; var cdPista = cell?.StringCellValue; if (cdPista?.Length != 5) { Console.WriteLine("FilaExcel: {1}. {0}.", $"cdPista Incorrecto ({cdPista})", row); continue; } int i; if (!int.TryParse(cdPista.Substring(0, 2), out i)) { Console.WriteLine("FilaExcel: {1}. {0}.", $"cdPista Incorrecto ({cdPista})", row); continue; } if (!int.TryParse(cdPista.Substring(3, 2), out i)) { Console.WriteLine("FilaExcel: {1}. {0}.", $"cdPista Incorrecto ({cdPista})", row); continue; } if (row >= 10000) { break; } var itemdata = new ItemData() { CdPista = cdPista, Coleccion = "" }; if (ColumnInterprete > -1) { cell = xlRow.GetCell(ColumnInterprete); itemdata.Interprete = cell?.StringCellValue; } if (ColumnTitulo > -1) { cell = xlRow.GetCell(ColumnTitulo); itemdata.Titulo = cell?.StringCellValue; } var resultadoOperacion = searchFile.GetFile(itemdata); if (!resultadoOperacion.Ok) { filasError++; Console.WriteLine("FilaExcel: {1}. {0}.", resultadoOperacion.Mensaje, row); continue; } cell = xlRow.GetCell(ColumnLink) ?? xlRow.CreateCell(ColumnLink, CellType.String); var cellHyperlink = new HSSFHyperlink(HyperlinkType.File) { Address = resultadoOperacion.Mensaje.Substring(1) }; cell.Hyperlink = cellHyperlink; cell.SetCellValue("u"); //en font Windings 3 es play cell.CellStyle = linkStyle; if (ColumnCarpeta >= 0) { cell = xlRow.GetCell(ColumnCarpeta) ?? xlRow.CreateCell(ColumnCarpeta, CellType.String); cell.SetCellValue(itemdata.Carpeta); } if (ColumnMusica >= 0) { cell = xlRow.GetCell(ColumnMusica) ?? xlRow.CreateCell(ColumnMusica, CellType.String); cell.SetCellValue(itemdata.Archivo); } } using (var fs = File.OpenWrite(Excel)) { hssfWorkbook.Write(fs); } return(new ResultadoOperacion { Ok = true, Mensaje = $"Se procesaron {filas} del excel, de las cuales {filasError} filas no se encontro el archivo de música." }); }