예제 #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));
        }
예제 #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);
        }
예제 #3
0
        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);
        }
예제 #4
0
 public static void WriteCsvValue(this HttpResponse writer, object value, bool forceQuotes = false)
 {
     writer.Write(CsvUtility.ExportValue(value, forceQuotes));
 }
예제 #5
0
 static async ValueTask <IEnumerable <string>?> GetNextRowAsyncCore(StreamReader source)
 => source.EndOfStream ? null : CsvUtility.GetLine(await source.ReadLineAsync().ConfigureAwait(false));