public void AddLine(string srcLine) { if (!String.IsNullOrEmpty(srcLine)) { lines.Add(CsvLine.Load(srcLine)); } }
public void AddLine(CsvLine line) { if (line != null) { lines.Add(line); } }
public static CsvLine Load(string srcText, char delimeter = ',') { CsvLine line = new CsvLine(); bool quote = false; char escapeChar = '~'; string value = ""; string[] tempValues; if (escapeChar == delimeter) { if (!srcText.Contains('^')) { escapeChar = '^'; } } if (!String.IsNullOrEmpty(srcText)) { for (int x = 0; x < srcText.Length; x++) { if (srcText[x] == '\u0022') { quote = !quote; } if (quote) { value += srcText[x]; } else { if (srcText[x] == delimeter) { value += escapeChar; } else { if (srcText[x] != ' ') { value += srcText[x]; } } } } } tempValues = value.Split(new char[] { escapeChar }, StringSplitOptions.RemoveEmptyEntries); foreach (string v in tempValues) { line.AddValue(v.Trim()); } return(line); }
public bool RemoveLine(CsvLine line) { bool removed = false; if (Lines.Contains(line)) { lines.Remove(line); removed = true; } return(removed); }
public bool UpdateLine(CsvLine line) { bool updated = false; if (Lines.Contains(line)) { int index = lines.IndexOf(line); lines.RemoveAt(index); lines.Insert(index, line); updated = true; } return(updated); }
public static CSV Load(string srcText, char delimeter = ',') { if (IsSrcValid(srcText)) { List <CsvLine> lines = new List <CsvLine>(); string[] srcLines = srcText.Split('\n'); foreach (string l in srcLines) { lines.Add(CsvLine.Load(l.Trim(), delimeter)); } CSV csv = new CSV(); csv.lines.AddRange(lines); return(csv); } return(null); }
public static CSV JsonToCsv(string jsonSrc) { CSV csv = null; if (Json.IsSrcValid(jsonSrc)) { Json json = Json.Load(jsonSrc); List <CsvLine> lines = new List <CsvLine>(); CsvLine fields = new CsvLine(); foreach (JsonKeyValue keyValue in json.Objects.First().KeyValues) { fields.AddValue(keyValue.Key); if (keyValue == json.Objects.First().KeyValues.Last()) { lines.Add(fields); } } foreach (JsonObject obj in json.Objects) { CsvLine line = new CsvLine(); foreach (JsonKeyValue keyValue in obj.KeyValues) { line.AddValue(keyValue.Value.ToString()); } lines.Add(line); } csv = new CSV(); csv.AddLines(lines); } return(csv); }
private void btnCsvConvert_Click(object sender, RoutedEventArgs e) { if (txtRichCSV.Document.Blocks.Count > 0) { TextRange textRange = new TextRange(txtRichCSV.Document.ContentStart, txtRichCSV.Document.ContentEnd); string[] lines = textRange.Text.Split('\n'); csv.ClearLines(); foreach (string l in lines) { string[] values = l.Split('\t'); string lineSrc = ""; foreach (string v in values) { if (!String.IsNullOrEmpty(v)) { lineSrc += v; } if (v != values.Last()) { lineSrc += ","; } } if (!String.IsNullOrEmpty(lineSrc)) { csv.AddLine(CsvLine.Load(lineSrc)); } } json = Converter.CsvToJson(csv); ClearBoxesAndDisableButtons(); PopulateBoxes(); } }