/// <summary> /// Iterate through all lines in this CSV file /// </summary> /// <returns></returns> public IEnumerator <string[]> GetEnumerator() { return(CSV.ParseStream(_stream, _settings).GetEnumerator()); }
/// <summary> /// Read this file into a data table in memory /// </summary> /// <returns></returns> public DataTable ReadAsDataTable() { var dt = new DataTable(); string[] firstLine = null; // File contains column names - so name each column properly if (Headers == null) { var rawLine = _stream.ReadLine(); firstLine = CSV.ParseLine(rawLine, _settings); var list = new List <string>(); for (var i = 0; i < firstLine.Length; i++) { list.Add($"Column{i}"); } this.Headers = list.ToArray(); } // Add headers var numColumns = Headers.Length; foreach (var t in Headers) { dt.Columns.Add(new DataColumn(t, typeof(string))); } // If we had to read the first line to get dimensions, add it var row_num = 1; if (firstLine != null) { dt.Rows.Add(firstLine); row_num++; } // Start reading through the file foreach (var line in CSV.ParseStream(_stream, _settings)) { // Does this line match the length of the first line? if (line.Length != numColumns) { if (!_settings.IgnoreDimensionErrors) { throw new Exception($"Line #{row_num} contains {line.Length} columns; expected {numColumns}"); } else { // Add as best we can - construct a new line and make it fit var list = new List <string>(); list.AddRange(line); while (list.Count < numColumns) { list.Add(""); } dt.Rows.Add(list.GetRange(0, numColumns).ToArray()); } } else { dt.Rows.Add(line); } // Keep track of where we are in the file row_num++; } // Here's your data table return(dt); }
/// <summary> /// Iterate through all lines in this CSV file /// </summary> /// <returns>An array of all data columns in the line</returns> public IEnumerable <string[]> Lines() { return(CSV.ParseStream(_stream, _settings)); }