CsvConverterSaveTSource(IEnumerableTSource, String, CsvFromTSource, Char, Encoding, IReadOnlyCollectionString) Method

Saves a collection of TSource instances as a CSV file with header row.

Definition

Namespace: FolkerKinzel.CsvTools.Mappings
Assembly: FolkerKinzel.CsvTools.Mappings (in FolkerKinzel.CsvTools.Mappings.dll) Version: 1.1.0+1263e8243dc2cd78095f678f813d7d9c52ea4315
C#
public static void Save<TSource>(
	IEnumerable<TSource> data,
	string filePath,
	CsvFrom<TSource> converter,
	char delimiter = ',',
	Encoding? textEncoding = null,
	IReadOnlyCollection<string?>? columnNames = null
)

Parameters

data  IEnumerableTSource
The data to save as CSV file. Each item will be represented with a CSV row.
filePath  String
File path of the CSV file.
converter  CsvFromTSource
An object that converts a TSource instance to a CSV row.
delimiter  Char  (Optional)
The field separator character.
textEncoding  Encoding  (Optional)
The text encoding to be used or null for UTF8.
columnNames  IReadOnlyCollectionString  (Optional)

A collection of column names for the header to be written, or null to use the PropertyNames of the CsvMapping as column names.

The collection determines the order in which the columns appear in the CSV file.

The collection will be copied. If the collection contains null values, empty strings, or white space, these are replaced by automatically generated column names. Column names cannot appear twice. By default the comparison is case-sensitive but it will be reset to a case-insensitive comparison if the column names are also unique when treated case-insensitive.

Type Parameters

TSource
Generic type parameter for the data type to write as CSV row.

Remarks

Creates a new CSV file. If the target file already exists, it is truncated and overwritten.

When exchanging CSV data with Excel, the appropriate arguments can be determined with GetExcelArguments.

Exceptions

ArgumentNullExceptiondata, or filePath, or converter is null.
ArgumentException

filePath is not a valid file path.

- or -

columnNames is not null and a column name in columnNames occurs twice.

IOExceptionI/O error.

See Also