/// <summary> /// Serialize an array of objects to CSV format /// </summary> /// <typeparam name="T">The type of objects to serialize from this CSV</typeparam> /// <param name="list">The array of objects to serialize</param> /// <param name="stream">The stream to which we will send this CSV text</param> /// <param name="settings">The CSV settings to use when exporting this array (Default: CSV)</param> /// <returns>The completed CSV string representing one line per element in list</returns> public static void Serialize <T>(IEnumerable <T> list, Stream stream, CSVSettings settings = null) where T : class, new() { using (var cw = new CSVWriter(stream, settings)) { cw.Serialize(list); } }
/// <summary> /// Serialize an array of objects to CSV format /// </summary> /// <typeparam name="T">The type of objects to serialize from this CSV</typeparam> /// <param name="list">The array of objects to serialize</param> /// <param name="stream">The stream to which we will send this CSV text</param> /// <param name="settings">The CSV settings to use when exporting this array (Default: CSV)</param> /// <returns>The completed CSV string representing one line per element in list</returns> public static Task SerializeAsync <T>(IAsyncEnumerable <T> list, Stream stream, CSVSettings settings = null) where T : class, new() { using (var cw = new CSVWriter(stream, settings)) { return(cw.SerializeAsync(list)); } }
public static void WriteToStream(this DataTable dt, StreamWriter sw, bool save_column_names, char delim = DEFAULT_DELIMITER, char qual = DEFAULT_QUALIFIER) #endif { using (CSVWriter cw = new CSVWriter(sw, delim, qual)) { cw.Write(dt, save_column_names); } }
public static void WriteToStream <T>(this IEnumerable <T> list, StreamWriter sw, bool save_column_names, char delim = DEFAULT_DELIMITER, char qual = DEFAULT_QUALIFIER) #endif { using (CSVWriter cw = new CSVWriter(sw, delim, qual)) { cw.WriteObjects(list, save_column_names); } }
/// <summary> /// Take a CSV file and chop it into multiple chunks of a specified maximum size. /// </summary> /// <param name="filename">The input filename to chop</param> /// <param name="out_folder">The folder where the chopped CSV will be saved</param> /// <param name="maxLinesPerFile">The maximum number of lines to put into each file</param> /// <param name="settings">The CSV settings to use when chopping this file into chunks (Default: CSV)</param> /// <returns>Number of files chopped</returns> public static int ChopFile(string filename, string out_folder, int maxLinesPerFile, CSVSettings settings = null) { // Default settings if (settings == null) { settings = CSVSettings.CSV; } // Let's begin parsing int file_id = 1; int line_count = 0; string file_prefix = Path.GetFileNameWithoutExtension(filename); string ext = Path.GetExtension(filename); CSVWriter cw = null; StreamWriter sw = null; // Read in lines from the file using (var sr = new StreamReader(filename)) { using (CSVReader cr = new CSVReader(sr, settings)) { // Okay, let's do the real work foreach (string[] line in cr.Lines()) { // Do we need to create a file for writing? if (cw == null) { string fn = Path.Combine(out_folder, file_prefix + file_id.ToString() + ext); sw = new StreamWriter(fn); cw = new CSVWriter(sw, settings); if (settings.HeaderRowIncluded) { cw.WriteLine(cr.Headers); } } // Write one line cw.WriteLine(line); // Count lines - close the file if done line_count++; if (line_count >= maxLinesPerFile) { cw.Dispose(); cw = null; file_id++; line_count = 0; } } } } // Ensore the final CSVWriter is closed properly if (cw != null) { cw.Dispose(); cw = null; } return(file_id); }
/// <summary> /// Write the data table to a stream in CSV format /// </summary> /// <param name="dt">The data table to write</param> /// <param name="sw">The stream where the CSV text will be written</param> /// <param name="settings">The CSV settings to use when exporting this DataTable (Default: CSV)</param> public static void WriteToStream(this DataTable dt, StreamWriter sw, CSVSettings settings = null) { using (CSVWriter cw = new CSVWriter(sw, settings)) { cw.Write(dt); } }
/// <summary> /// Serialize an object array to a stream in CSV format /// </summary> /// <param name="list">The object array to write</param> /// <param name="sw">The stream where the CSV text will be written</param> /// <param name="settings">The CSV settings to use (Default: CSV)</param> public static void WriteToStream <T>(this IEnumerable <T> list, StreamWriter sw, CSVSettings settings = null) where T : class, new() { using (CSVWriter cw = new CSVWriter(sw, settings)) { cw.WriteArray <T>(list); } }
/// <summary> /// Saves an array of objects to a CSV string in memory. /// </summary> /// <typeparam name="T">The type of objects to serialize from this CSV.</typeparam> /// <param name="list">The array of objects to serialize.</param> /// <param name="save_column_names">Set to true if you wish the first line of the CSV to contain the field names.</param> /// <param name="force_qualifiers">Set to true to force qualifier characters around each field.</param> /// <param name="delim">The CSV field delimiter character.</param> /// <param name="qual">The CSV text qualifier character.</param> /// <returns>The CSV string.</returns> public static string SaveArray <T>(IEnumerable <T> list, bool save_column_names = true, bool force_qualifiers = false, char delim = CSV.DEFAULT_DELIMITER, char qual = CSV.DEFAULT_QUALIFIER) where T : class, new() { using (var ms = new MemoryStream()) { var sw = new StreamWriter(ms); var cw = new CSVWriter(sw, delim, qual); cw.WriteObjects <T>(list, save_column_names, force_qualifiers); sw.Flush(); ms.Position = 0; using (var sr = new StreamReader(ms)) { return(sr.ReadToEnd()); } } }
public static string WriteToString <T>(this IEnumerable <T> list, bool save_column_names, char delim = DEFAULT_DELIMITER, char qual = DEFAULT_QUALIFIER) #endif { using (var ms = new MemoryStream()) { var sw = new StreamWriter(ms); var cw = new CSVWriter(sw, delim, qual); cw.WriteObjects(list, save_column_names); sw.Flush(); ms.Position = 0; using (var sr = new StreamReader(ms)) { return(sr.ReadToEnd()); } } }
/// <summary> /// Write a DataTable to a string in CSV format /// </summary> /// <param name="dt">The datatable to write</param> /// <param name="settings">The CSV settings to use when exporting this DataTable (Default: CSV)</param> /// <returns>The CSV string representing the object array.</returns> public static string WriteToString(this DataTable dt, CSVSettings settings = null) { using (var ms = new MemoryStream()) { var sw = new StreamWriter(ms); var cw = new CSVWriter(sw, settings); cw.Write(dt); sw.Flush(); ms.Position = 0; using (var sr = new StreamReader(ms)) { return(sr.ReadToEnd()); } } }
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)); } }
/// <summary> /// Take a CSV file and chop it into multiple chunks of a specified maximum size. /// </summary> /// <param name="filename"></param> /// <param name="out_folder"></param> /// <param name="first_row_are_headers"></param> /// <param name="max_lines_per_file"></param> /// <returns>Number of files chopped</returns> public static int ChopFile(string filename, string out_folder, bool first_row_are_headers, int max_lines_per_file, char delim = CSV.DEFAULT_DELIMITER, char qual = CSV.DEFAULT_QUALIFIER) { int file_id = 1; int line_count = 0; string file_prefix = Path.GetFileNameWithoutExtension(filename); string ext = Path.GetExtension(filename); CSVWriter cw = null; // Read in lines from the file using (CSVReader cr = new CSVReader(filename, delim, qual, first_row_are_headers)) { // Okay, let's do the real work foreach (string[] line in cr.Lines()) { // Do we need to create a file for writing? if (cw == null) { string fn = Path.Combine(out_folder, file_prefix + file_id.ToString() + ext); cw = new CSVWriter(fn, delim, qual); if (first_row_are_headers) { cw.WriteLine(cr.Headers); } } // Write one line cw.WriteLine(line); // Count lines - close the file if done line_count++; if (line_count >= max_lines_per_file) { cw.Dispose(); cw = null; file_id++; line_count = 0; } } } // Ensore the final CSVWriter is closed properly if (cw != null) { cw.Dispose(); cw = null; } return(file_id); }
/// <summary> /// Serialize an array of objects to CSV format /// </summary> /// <typeparam name="T">The type of objects to serialize from this CSV</typeparam> /// <param name="list">The array of objects to serialize</param> /// <param name="settings">The CSV settings to use when exporting this array (Default: CSV)</param> /// <returns>The completed CSV string representing one line per element in list</returns> public static string Serialize <T>(IEnumerable <T> list, CSVSettings settings = null) where T : class, new() { if (settings == null) { settings = CSVSettings.CSV; } using (var ms = new MemoryStream()) { using (var cw = new CSVWriter(ms, settings)) { cw.Serialize(list); } var rawString = settings.Encoding.GetString(ms.ToArray()); return(RemoveByteOrderMarker(rawString)); } }