コード例 #1
0
 public void AddLine(string srcLine)
 {
     if (!String.IsNullOrEmpty(srcLine))
     {
         lines.Add(CsvLine.Load(srcLine));
     }
 }
コード例 #2
0
 public void AddLine(CsvLine line)
 {
     if (line != null)
     {
         lines.Add(line);
     }
 }
コード例 #3
0
        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);
        }
コード例 #4
0
        public bool RemoveLine(CsvLine line)
        {
            bool removed = false;

            if (Lines.Contains(line))
            {
                lines.Remove(line);
                removed = true;
            }

            return(removed);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        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();
            }
        }