/* * 2006 - 2018 Ted Spence, http://tedspence.com * License: http://www.apache.org/licenses/LICENSE-2.0 * Home page: https://github.com/tspence/csharp-csv-reader */ using System.Data; using System.IO; namespace CSVNET { /// /// Code to assist in working with DataTable objects and CSV sources /// public static class CSVDataTable { /// /// Read in a single CSV file into a datatable in memory /// /// /// The CSV settings to use when exporting this array (Default: CSV) /// An data table of strings that were retrieved from the CSV file. public static DataTable FromFile(string filename, CSVSettings settings = null) { using (var sr = new StreamReader(filename)) { return FromStream(sr, settings); } } /// /// Read in a single CSV file into a datatable in memory /// /// The stream source from which to load the datatable. /// The CSV settings to use when exporting this array (Default: CSV) /// An data table of strings that were retrieved from the CSV file. public static DataTable FromStream(StreamReader stream, CSVSettings settings = null) { using (var cr = new CSVReader(stream, settings)) { return cr.ReadAsDataTable(); } } /// /// Convert a CSV file (in string form) into a data table /// /// /// The CSV settings to use when exporting this array (Default: CSV) /// public static DataTable FromString(string source, CSVSettings settings = null) { if (settings == null) { settings = CSVSettings.CSV; } var byteArray = settings.Encoding.GetBytes(source); using (var stream = new MemoryStream(byteArray)) { using (var cr = new CSVReader(stream, settings)) { return cr.ReadAsDataTable(); } } } /// /// Write a data table to disk at the designated file name in CSV format /// /// /// /// The CSV settings to use when exporting this DataTable (Default: CSV) #if NET2_0 public static void WriteToFile(DataTable dt, string filename, CSVSettings settings = null) #else public static void WriteToFile(this DataTable dt, string filename, CSVSettings settings = null) #endif { if (settings == null) { settings = CSVSettings.CSV; } using (var fs = new FileStream(filename, FileMode.CreateNew)) { using (var sw = new StreamWriter(fs, settings.Encoding)) { WriteToStream(dt, sw, settings); } } } /// /// Write the data table to a stream in CSV format /// /// The data table to write /// The stream where the CSV text will be written /// The CSV settings to use when exporting this DataTable (Default: CSV) #if NET2_0 public static void WriteToStream(DataTable dt, StreamWriter sw, CSVSettings settings = null) #else public static void WriteToStream(this DataTable dt, StreamWriter sw, CSVSettings settings = null) #endif { using (var cw = new CSVWriter(sw, settings)) { cw.Write(dt); } } /// /// Write a DataTable to a string in CSV format /// /// The datatable to write /// The CSV settings to use when exporting this DataTable (Default: CSV) /// The CSV string representing the object array. #if NET2_0 public static string WriteToString(DataTable dt, CSVSettings settings = null) #else public static string WriteToString(this DataTable dt, CSVSettings settings = null) #endif { if (settings == null) { settings = CSVSettings.CSV; } using (var ms = new MemoryStream()) { var cw = new CSVWriter(ms, settings); cw.Write(dt); var rawString = settings.Encoding.GetString(ms.ToArray()); return CSV.RemoveByteOrderMarker(rawString); } } } }