CsvWrite(DataTable, TextWriter, Char, IFormatProvider, IEnumerableString, String) Method

Writes the content of a DataTable as CSV with header row.

Definition

Namespace: FolkerKinzel.CsvTools
Assembly: FolkerKinzel.CsvTools (in FolkerKinzel.CsvTools.dll) Version: 2.0.1+2345335399184346d9b2cc992ed5c814406052c1
C#
public static void Write(
	DataTable dataTable,
	TextWriter textWriter,
	char delimiter = ',',
	IFormatProvider? formatProvider = null,
	IEnumerable<string>? csvColumnNames = null,
	string? format = null
)

Parameters

dataTable  DataTable
The DataTable whose content is written.
textWriter  TextWriter
The TextWriter to be used.
delimiter  Char  (Optional)
The field separator character.
formatProvider  IFormatProvider  (Optional)

The provider to use to format the value.

- or -

A null reference for InvariantCulture.

csvColumnNames  IEnumerableString  (Optional)

A collection of ColumnNames from dataTable that allows to select the DataColumns to export and to determine their order in the CSV file, or null to save the whole DataTable with its current column order.

Each item in this collection MUST be a ColumnName in dataTable.

format  String  (Optional)

A format String to use for all items that implement IFormattable.

- or -

A null reference to use the default format for each item.

Remarks

For serialization ToString(String, IFormatProvider) is used if the item implements IFormattable, otherwise ToString.

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

Exceptions

ArgumentNullExceptiondataTable or textWriter is null.
ArgumentExceptioncsvColumnNames contains an item that is not a ColumnName in dataTable.
ArgumentOutOfRangeExceptiondelimiter is either the double quotes " or a line break character ('\r' or '\n').
IOExceptionI/O error.

See Also