CsvWriter Class
Writes CSV data to a text writer.
public class CsvWriterExample
using var writer = new StringWriter();
var csvWriter = new CsvWriter(writer) { EndOfLine = "\n" };
await csvWriter.WriteRowAsync("Name", "Age");
await csvWriter.WriteRowAsync("John", "30");
await csvWriter.WriteRowAsync("Jane", "25");
Assert.Equal("Name,Age\nJohn,30\nJane,25", writer.ToString());Constructors
| Name | Description |
|---|---|
| CsvWriter(TextWriter) | Initializes a new instance of the CsvWriter class with the specified TextWriter. |
CsvWriter(TextWriter)
CsvWriter.csInitializes a new instance of the CsvWriter class with the specified TextWriter.
public CsvWriter(TextWriter textReader)Parameters
textReaderTextWriter- The
TextWriterto write CSV data to.
Exceptions
ArgumentNullExceptiontextReaderisnull.
Properties
| Name | Description |
|---|---|
| BaseWriter | Gets the underlying TextWriter used to write the CSV data. |
| EndOfLine | Gets or sets the end-of-line string. Default is Environment.NewLine. |
| Quote | Gets or sets the character used to quote values that contain special characters. Default is a double quote ("). |
| Separator | Gets or sets the character used to separate values in a row. Default is a comma (,). |
BaseWriter
CsvWriter.csGets the underlying TextWriter used to write the CSV data.
public TextWriter BaseWriter { get; }Property value
EndOfLine
CsvWriter.csGets or sets the end-of-line string. Default is Environment.NewLine.
public string EndOfLine { get; set; }Property value
Quote
CsvWriter.csSeparator
CsvWriter.csMethods
| Name | Description |
|---|---|
| BeginRowAsync() | Begins a new row. This method must be called before writing values using CsvWriter.WriteValueAsync or CsvWriter.WriteValuesAsync. |
| WriteRowAsync(IEnumerable<string?>) | Writes a complete row with the specified values. |
| WriteRowAsync(string?[]) | Writes a complete row with the specified values. |
| WriteValueAsync(string?) | Writes a single value to the current row. Call CsvWriter.BeginRowAsync before calling this method. |
| WriteValuesAsync(IEnumerable<string?>) | Writes multiple values to the current row. Call CsvWriter.BeginRowAsync before calling this method. |
| WriteValuesAsync(string?[]) | Writes multiple values to the current row. Call CsvWriter.BeginRowAsync before calling this method. |
BeginRowAsync()
CsvWriter.csBegins a new row. This method must be called before writing values using CsvWriter.WriteValueAsync or CsvWriter.WriteValuesAsync.
public Task BeginRowAsync()Returns
Task A task that represents the asynchronous operation.
WriteRowAsync overloads
WriteRowAsync(IEnumerable<string?>)
CsvWriter.csWrites a complete row with the specified values.
net10.0
public Task WriteRowAsync(IEnumerable<string?> values)net11.0
public Task WriteRowAsync(IEnumerable<string?> values)Parameters
valuesIEnumerable<string?>- The values to write.
Returns
Task A task that represents the asynchronous operation.
WriteRowAsync(string?[])
CsvWriter.csWriteValueAsync(string?)
CsvWriter.csWrites a single value to the current row. Call CsvWriter.BeginRowAsync before calling this method.
WriteValuesAsync overloads
WriteValuesAsync(IEnumerable<string?>)
CsvWriter.csWrites multiple values to the current row. Call CsvWriter.BeginRowAsync before calling this method.
net10.0
public Task WriteValuesAsync(IEnumerable<string?> values)net11.0
public Task WriteValuesAsync(IEnumerable<string?> values)Parameters
valuesIEnumerable<string?>- The values to write.
Returns
Task A task that represents the asynchronous operation.
WriteValuesAsync(string?[])
CsvWriter.csWrites multiple values to the current row. Call CsvWriter.BeginRowAsync before calling this method.
Parameters
valuesstring?[]- The values to write.
Returns
Task A task that represents the asynchronous operation.