Example #1
0
        public static void WriteValue(TextWriter writer, object?value = null, bool forceQuotes = false)
        {
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            Contract.EndContractBlock();

            writer.Write(CsvUtility.ExportValue(value, forceQuotes));
        }
Example #2
0
        public static bool TryGetNextRow(StreamReader source, out IEnumerable <string> row)
        {
            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            Contract.EndContractBlock();

            if (!source.EndOfStream)
            {
                row = CsvUtility.GetLine(source.ReadLine());
                return(true);
            }

            row = null !;
            return(false);
        }
        public static DataTable GetDataTable(string csv, bool firstRowIsHeader = false)
        {
            string[][] data = CsvUtility.GetArray(csv, out int maxColumns);

            var t = new DataTable();

            if (data.Any())
            {
                if (firstRowIsHeader)
                {
                    var firstRow = data.First();
                    for (int i = 0; i < maxColumns; i++)
                    {
                        t.Columns.Add(firstRow[i], typeof(string));
                    }
                }
                else
                {
                    for (int i = 0; i < maxColumns; i++)
                    {
                        t.Columns.Add("Column" + i.ToString("00"), typeof(string));
                    }
                }

                for (int n = firstRowIsHeader ? 1 : 0; n < data.Length; n++)
                {
                    var     s = data[n];
                    DataRow r = t.NewRow();
                    for (int i = 0; i < s.Length; i++)
                    {
                        r["Column" + i.ToString("00")] = s[i];
                    }
                    t.Rows.Add(r);
                }
            }

            return(t);
        }
Example #4
0
 public static void WriteCsvValue(this HttpResponse writer, object value, bool forceQuotes = false)
 {
     writer.Write(CsvUtility.ExportValue(value, forceQuotes));
 }
Example #5
0
 static async ValueTask <IEnumerable <string>?> GetNextRowAsyncCore(StreamReader source)
 => source.EndOfStream ? null : CsvUtility.GetLine(await source.ReadLineAsync().ConfigureAwait(false));