/// <summary> /// This method tries to save given values into given file. /// </summary> /// <remarks> /// This method determines the file existence and performs file /// deletion if requested. Thereafter, the file content is handled /// by creating and processing a stream. /// </remarks> /// <param name="values"> /// The list of values to be written to the CSV file. /// </param> /// <param name="filename"> /// The fully qualified path of the output file. /// </param> /// <param name="settings"> /// The settings to be used to generate the output of the CSV file. /// </param> /// <param name="overwrite"> /// If true, then a possible existing file is overwritten. Otherwise, /// an exception is thrown if a file with the same name already exists. /// </param> /// <exception cref="ArgumentException"> /// This exception is thrown either in case of given values are invalid /// or if given filename is invalid. /// </exception> /// <exception cref="InvalidOperationException"> /// This exception is thrown if overwrite mode is disabled and given /// file already exists or in case of given file could not be deleted. /// Another reason could be the case when property parsing fails. /// </exception> /// <exception cref="NotSupportedException"> /// This exception is thrown in case of confirming and ordering the column /// offsets fails. /// </exception> public static void Save(IEnumerable <TInstance> values, String filename, CsvSettings settings, Boolean overwrite) { if (String.IsNullOrWhiteSpace(filename)) { throw new ArgumentException("The filename should not be empty.", nameof(filename)); } if (!overwrite && File.Exists(filename)) { throw new InvalidOperationException($"File {filename} already exists and cannot be overwritten with disabled overwrite mode."); } if (File.Exists(filename)) { try { File.Delete(filename); } catch (Exception exception) { throw new InvalidOperationException($"Could not delete file {filename}. See inner exception for more details.", exception); } } using (Stream stream = File.Create(filename)) { CsvExporter <TInstance> .Save(values, stream, settings); } }
/// <summary> /// This method tries to save given values into given stream. /// </summary> /// <remarks> /// Please keep in mind, a textual treatment is only applicable for string /// data types, not matter what the actual value of the 'textual' property /// of given settings is. Additionally, a textual treatment is never applied /// to the header, in case of it is processed. /// </remarks> /// <param name="values"> /// The list of values to be written into given stream. /// </param> /// <param name="stream"> /// The stream to write given values into. /// </param> /// <param name="settings"> /// The settings to be used to generate the CSV output. /// </param> /// <exception cref="ArgumentException"> /// This exception is thrown either in case of given values are invalid /// or if given stream does not allow write access. /// </exception> /// <exception cref="ArgumentNullException"> /// This exception is thrown if given stream is <null> or if given /// settings are <null>. /// </exception> /// <exception cref="InvalidOperationException"> /// This exception is thrown in case of property parsing fails. /// </exception> /// <exception cref="NotSupportedException"> /// This exception is thrown in case of confirming and ordering the column /// offsets fails. /// </exception> public static void Save(IEnumerable <TInstance> values, Stream stream, CsvSettings settings) { if (values == null || !values.Any()) { throw new ArgumentException("Values to write may contain at least one record.", nameof(values)); } if (stream == null) { throw new ArgumentNullException(nameof(stream), $"The stream to write the data into is invalid."); } if (!stream.CanWrite) { throw new ArgumentException("No write access to given stream.", nameof(stream)); } if (settings == null) { throw new ArgumentNullException(nameof(settings), "The CSV settings are invalid."); } TypeDescriptor descriptor = TypeProcessor.LoadDescriptor <TInstance>(); using (StreamWriter writer = new StreamWriter(stream, settings.Encoding)) { if (settings.Heading) { CsvExporter <TInstance> .WriteHead(writer, settings.Separator, false, descriptor.Settings); } foreach (TInstance value in values) { CsvExporter <TInstance> .WriteLine(writer, settings.Separator, settings.Textual, settings.Culture, settings.Mappings, CsvExporter <TInstance> .BuildLine(descriptor.Settings, value)); } } stream.Flush(); }
/// <summary> /// This method tries to save given values into given stream. /// </summary> /// <remarks> /// This method performes saving of data with default settings. Using /// default settings means that the header is written. Further, needed /// header information is taken from the column attributes or from property /// names. Finally, a special textual treatment is not applied. /// </remarks> /// <param name="values"> /// The list of values to be written into given stream. /// </param> /// <param name="stream"> /// The stream to write given values into. /// </param> /// <exception cref="ArgumentException"> /// This exception is thrown either in case of given values are invalid /// or if given stream does not allow write access. /// </exception> /// <exception cref="ArgumentNullException"> /// This exception is thrown if given stream is <null>. /// </exception> /// <exception cref="InvalidOperationException"> /// This exception is thrown in case of property parsing fails. /// </exception> /// <exception cref="NotSupportedException"> /// This exception is thrown in case of confirming and ordering the column /// offsets fails. /// </exception> public static void Save(IEnumerable <TInstance> values, Stream stream) { CsvExporter <TInstance> .Save(values, stream, new CsvSettings()); }
/// <summary> /// This method tries to save given values into given file. /// </summary> /// <remarks> /// This method performes saving of data using given settings. /// But keep in mind, a possible existing file is overwritten. /// </remarks> /// <param name="values"> /// The list of values to be written to the CSV file. /// </param> /// <param name="filename"> /// The fully qualified path of the output file. /// </param> /// <param name="settings"> /// The settings to be used to generate the output of the CSV file. /// </param> /// <exception cref="ArgumentException"> /// This exception is thrown in case of given filename is invalid. /// </exception> /// <exception cref="InvalidOperationException"> /// This exception is thrown in case of given file could not be deleted. /// Another reason could be the case when property parsing fails. /// </exception> /// <exception cref="NotSupportedException"> /// This exception is thrown in case of confirming and ordering the column /// offsets fails. /// </exception> public static void Save(IEnumerable <TInstance> values, String filename, CsvSettings settings) { CsvExporter <TInstance> .Save(values, filename, settings, true); }
/// <summary> /// This method tries to save given values into given file. /// </summary> /// <remarks> /// This method performes saving of data with default settings. Using /// default settings means that the header is written and a possible /// existing file is overwritten. Further, needed header information is /// taken from column attributes or from property names. /// </remarks> /// <param name="values"> /// The list of values to be written to the CSV file. /// </param> /// <param name="filename"> /// The fully qualified path of the output file. /// </param> /// <exception cref="ArgumentException"> /// This exception is thrown in case of given filename is invalid. /// </exception> /// <exception cref="InvalidOperationException"> /// This exception is thrown in case of given file could not be deleted. /// Another reason could be the case when property parsing fails. /// </exception> /// <exception cref="NotSupportedException"> /// This exception is thrown in case of confirming and ordering the column /// offsets fails. /// </exception> public static void Save(IEnumerable <TInstance> values, String filename) { CsvExporter <TInstance> .Save(values, filename, new CsvSettings()); }