private static void Append(TextWriter fs, CsvOptions options, IList <object> fields) { for (int i = 0; i < fields.Count; i++) { if (i > 0) { fs.Write(options.Delimiter); } fs.Write(options.ValueToString(fields[i])); } fs.Write(Environment.NewLine); }
/// <summary> /// Simply dumps the DataTable contents to a delimited file. Only /// allows to set the delimiter. /// </summary> /// <param name="dt">The source Data Table</param> /// <param name="filename">The destination file.</param> /// <param name="options">The options used to write the file</param> public static void DataTableToCsv(DataTable dt, string filename, CsvOptions options) { using (var fs = new StreamWriter(filename, false, options.Encoding, DefaultWriteBufferSize)) { foreach (DataRow dr in dt.Rows) { object[] fields = dr.ItemArray; for (int i = 0; i < fields.Length; i++) { if (i > 0) { fs.Write(options.Delimiter); } fs.Write(options.ValueToString(fields[i])); } fs.Write(StringHelper.NewLine); } fs.Close(); } }
/// <summary>Simply dumps the DataTable contents to a delimited file. Only allows to set the delimiter.</summary> /// <param name="dt">The source Data Table</param> /// <param name="filename">The destination file.</param> /// <param name="options">The options used to write the file</param> public static void DataTableToCsv(DataTable dt, string filename, CsvOptions options) { using (StreamWriter fs = new StreamWriter(filename, false, options.Encoding)) { for(int i = 0; i < dt.Columns.Count; i++) { if (i > 0) fs.Write(options.Delimiter); fs.Write(dt.Columns[i].ColumnName); } fs.Write(StringHelper.NewLine); foreach (DataRow dr in dt.Rows) { object[] fields = dr.ItemArray; for(int i = 0; i < fields.Length; i++) { if (i > 0) fs.Write(options.Delimiter); string convertedValue = options.ValueToString(fields[i]); fs.Write(convertedValue.TrimEnd(new char[] { '\n', ' ', '\r' })); } fs.Write(StringHelper.NewLine); } fs.Close(); } }
/// <summary>Simply dumps the DataTable contents to a delimited file. Only allows to set the delimiter.</summary> /// <param name="dt">The source Data Table</param> /// <param name="filename">The destination file.</param> /// <param name="options">The options used to write the file</param> public static void DataTableToCsv(DataTable dt, string filename, CsvOptions options) { using (StreamWriter fs = new StreamWriter(filename, false, options.Encoding)) { foreach (DataRow dr in dt.Rows) { object[] fields = dr.ItemArray; for(int i = 0; i < fields.Length; i++) { if (i > 0) fs.Write(options.Delimiter); fs.Write(options.ValueToString(fields[i])); } fs.Write(StringHelper.NewLine); } fs.Close(); } }