public static bool GetDataFromTextFileSimple(string sep, string importTermsPasteBox, string inputTextFile, out List <TermObj> lstTermObjs, out string msg) { // term guid not imported // terms flat in file, no heirarchy // can only import terms flat, and term labels for the imported term msg = ""; lstTermObjs = new List <TermObj>(); var fileText = ""; try { if (!importTermsPasteBox.IsNull()) { fileText = importTermsPasteBox.Trim(); } else { fileText = System.IO.File.ReadAllText(inputTextFile); } fileText = GenUtil.NormalizeEol(fileText); var lines = fileText.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Distinct().ToList(); foreach (var line in lines) { // extract termname and optional labels var termId = Guid.NewGuid(); var termName = ""; var labels = new List <string>(); if (line.Contains(sep)) { var parts = line.Split(new string[] { sep }, StringSplitOptions.RemoveEmptyEntries).Distinct(); if (GenUtil.IsGuid(parts.ElementAt(0))) { // guid is first item, termname must be second (thus 2 item minimum) if (parts.Count() >= 2) { termId = GenUtil.SafeToGuid(parts.ElementAt(0)).Value; termName = parts.ElementAt(1).Trim(); labels = parts.Skip(2).Where(x => x.Trim().Length > 0).Select(x => x.Trim()).Distinct().ToList(); } else { // termname not found, don't import this line termName = ""; } } else { termName = parts.ElementAt(0).Trim(); labels = parts.Skip(1).Where(x => x.Trim().Length > 0).Select(x => x.Trim()).Distinct().ToList(); } labels.RemoveAll(x => x.Trim().ToLower() == termName.Trim().ToLower()); } else { termName = line.Trim(); } if (!termName.IsNull()) { lstTermObjs.Add(new TermObj() { termId = termId, termName = termName, labels = labels }); } } } catch (Exception ex) { msg = ex.ToString(); } return(msg == ""); }
public static bool GetDataFromExcelFileSimple(string inputFile, out List <TermObj> lstTermObjs, out string msg) { // term guid not imported // terms flat in file, no heirarchy // can only import terms flat, and term labels for the imported term msg = ""; lstTermObjs = new List <TermObj>(); try { using (ExcelPackage package = new ExcelPackage(new FileInfo(inputFile))) { // get the first worksheet in the workbook ExcelWorksheet worksheet = package.Workbook.Worksheets[1]; var loop = true; int i = 0; while (loop) { i++; // get first cell, being termname var firstCell = worksheet.Cells[i, 1].Value.SafeTrim(); if (firstCell.IsNull()) { loop = false; } else { var termName = ""; Guid termId; var labels = new List <string>(); int j; if (GenUtil.IsGuid(firstCell)) { j = 3; termId = GenUtil.SafeToGuid(firstCell).Value; termName = worksheet.Cells[i, 2].Value.SafeTrim(); } else { j = 2; termId = Guid.NewGuid(); termName = firstCell; } // make sure termname was found if (!termName.IsNull()) { // get labels (optional) while (true) { var cellj = worksheet.Cells[i, j].Value.SafeTrim(); if (cellj.IsNull()) { break; } else { labels.Add(cellj); } j++; } labels.RemoveAll(x => x.Trim().ToLower() == termName.Trim().ToLower()); lstTermObjs.Add(new TermObj() { termId = termId, termName = termName, labels = labels }); } } } // while } // using } catch (Exception ex) { msg = ex.ToString(); } return(msg == ""); }