CsvConverterSave(DataTable, String, CsvMapping, Char, Encoding, IEnumerableString) Method

Saves the content of a DataTable as a CSV file with header.

Definition

Namespace: FolkerKinzel.CsvTools.Mappings
Assembly: FolkerKinzel.CsvTools.Mappings (in FolkerKinzel.CsvTools.Mappings.dll) Version: 1.1.0+1263e8243dc2cd78095f678f813d7d9c52ea4315
C#
public static void Save(
	DataTable dataTable,
	string filePath,
	CsvMapping mapping,
	char delimiter = ',',
	Encoding? textEncoding = null,
	IEnumerable<string?>? csvColumnNames = null
)

Parameters

dataTable  DataTable
The DataTable whose content is written.
filePath  String
File path of the CSV file.
mapping  CsvMapping
The CsvMapping to be used.
delimiter  Char  (Optional)
The field separator character.
textEncoding  Encoding  (Optional)
The text encoding to be used or null for UTF8.
csvColumnNames  IEnumerableString  (Optional)

A collection of column names for the CSV header row to be written, or null to use the ColumnNames of dataTable as CSV 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.

Remarks

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

Each PropertyName of mapping MUST have a corresponding DataColumn in dataTable - corresponding in the Caption property (case-insensitive) and the accepted data type.

Effort must be taken that the PropertyNames in mapping are unique, even when treated case-insensitive.

The DynamicProperty instances in mapping don't need to match all columns of the DataTable or all columns of the CSV file (neither in number nor in order).

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

Exceptions

ArgumentNullExceptiondataTable, or filePath, or mapping is null.
ArgumentException

filePath is not a valid file path.

- or -

There is a DynamicProperty in mapping whose PropertyName finds no corresponding ColumnName in dataTable.

InvalidCastException A value in dataTable does not match the expected data type in mapping.
FormatException One of the TypeConverterT instances uses an invalid format string.
IOExceptionI/O error.
ObjectDisposedExceptionThe file was already closed.

See Also