Example #1
0
        public static string Encode(CsvData csvData, Dictionary <int, FieldFormatOption> formatOptions, string separator)
        {
            CsvEncoder encoder = new CsvEncoder();

            encoder.FormatOptions = formatOptions;
            encoder.Separator     = separator;
            return(encoder.EncodeCsvData(csvData));
        }
Example #2
0
        private string EncodeCsvData(CsvData csvData)
        {
            StringBuilder text = new StringBuilder();

            if (csvData.HasHeader)
            {
                text.AppendLine(EncodeRecord(csvData.Header));
            }
            foreach (CsvRecord record in csvData.Records)
            {
                text.AppendLine(EncodeRecord(record));
            }
            return(text.ToString());
        }
Example #3
0
        public static CsvData Decode(string text, bool hasHeader)
        {
            if (text == null)
            {
                throw new ArgumentNullException("text");
            }
            bool    success;
            Parser  parser  = new Parser();
            CsvData csvData = parser.ParseCsvData(new TextInput(text), out success);

            if (success)
            {
                if (hasHeader)
                {
                    csvData.Header = csvData.Records[0];
                    csvData.Records.RemoveAt(0);
                }
                return(csvData);
            }
            else
            {
                throw new Exception("There are syntax errors in the csv text.");
            }
        }
Example #4
0
 public static string Encode(CsvData csvData)
 {
     return(Encode(csvData, null));
 }
Example #5
0
 /// <summary>
 /// Encode CsvData with Format Options
 /// </summary>
 /// <param name="csvData"></param>
 /// <param name="formatOptions">FieldFormatOption dict that use 0 based field index as key</param>
 /// <returns></returns>
 public static string Encode(CsvData csvData, Dictionary <int, FieldFormatOption> formatOptions)
 {
     return(Encode(csvData, formatOptions, ","));
 }