Click or drag to resize
Accord.NET (logo)

CsvWriter Class

Writer for CSV data.
Inheritance Hierarchy
SystemObject
  Accord.IOCsvWriter

Namespace:  Accord.IO
Assembly:  Accord.IO (in Accord.IO.dll) Version: 3.8.0
Syntax
public class CsvWriter : IDisposable
Request Example View Source

The CsvWriter type exposes the following members.

Constructors
  NameDescription
Public methodCsvWriter(TextWriter, Char)
Initializes a new instance of the CsvWriter class.
Public methodCsvWriter(String, Char)
Initializes a new instance of the CsvWriter class.
Top
Properties
  NameDescription
Public propertyComment
Gets or sets the comment character indicating that a line is commented out.
Public propertyDelimiter
Gets or sets the delimiter character separating each field.
Public propertyEscape
Gets or sets the escape character letting insert quotation characters inside a quoted field.
Public propertyFormatProvider
Gets or sets the format provider to use when converting data-types to text representations. Default is to use CultureInfo.InvariantCulture.
Public propertyQuote
Gets or sets the quotation character wrapping every field.
Public propertyWriter
Gets the writer.
Top
Methods
  NameDescription
Public methodDispose
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Protected methodDispose(Boolean)
Releases unmanaged and - optionally - managed resources.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Finalizes an instance of the CsvWriter class.
(Overrides ObjectFinalize.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodStatic memberToText
Initializes a new instance of the CsvWriter class to write the CSV fields to a in-memory string.
Public methodWrite(DataTable)
Writes the specified table in a CSV format.
Public methodWriteT(T)
Writes the specified matrix in CSV format.
Public methodWriteT(T)
Writes the specified matrix in CSV format.
Public methodWriteHeaders(DataTable)
Writes the column names of a data table as the headers of the CSV file.
Public methodWriteHeaders(String)
Writes the column names of a data table as the headers of the CSV file.
Public methodWriteLineT(T)
Writes the specified fields in a CSV format.
Public methodWriteLineT(T, String)
Writes the specified fields in a CSV format.
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(Type)Overloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Top
Examples

The following example shows how to use CsvWriter to write a matrix in .csv format.

// Tell where to store the matrix (adjust to your own liking)
string filename = Path.Combine(Path.GetTempPath(), "matrix.csv");

// Let's say we want to store a multidimensional array
double[,] values =
{
    { 1,  2,  3,  4 },
    { 5,  6,  7,  8 },
    { 9, 10, 11, 12 },
};

// Create a new writer and write the values to disk
using (CsvWriter writer = new CsvWriter(filename))
{
    writer.WriteHeaders("a", "b", "c", "d"); // this is optional
    writer.Write(values);
}

// Afterwards, we could read them back using
var reader = new CsvReader(filename, hasHeaders: true);
double[,] sameMatrix = reader.ToMatrix();

The following example shows how to use CsvWriter to write a jagged array in .csv format.

// Tell where to store the matrix (adjust to your own liking)
string filename = Path.Combine(Path.GetTempPath(), "jagged.csv");

// Let's say we want to store a jagged array
double[][] values =
{
    new double[] { 1,  2,  3,  4 },
    new double[] { 5,  6,  7,  8 },
    new double[] { 9, 10, 11, 12 },
};

// Create a new writer and write the values to disk
using (CsvWriter writer = new CsvWriter(filename))
{
    writer.WriteHeaders("a", "b", "c", "d"); // this is optional
    writer.Write(values);
}

// Afterwards, we could read them back using
var reader = new CsvReader(filename, hasHeaders: true); 
double[][] sameMatrix = reader.ToJagged();

The following example shows how to use CsvWriter to write a DataTable in .csv format.

// Tell where to store the data table (adjust to your own liking)
string filename = Path.Combine(Path.GetTempPath(), "table.csv");

// Let's say we have the following data table
DataTable table = new DataTable("My table");
table.Columns.Add("Name");
table.Columns.Add("Age");
table.Columns.Add("City");

// Let's add some rows for these columns
table.Rows.Add("John", 42, "New York");
table.Rows.Add("Josephine", 25, "Grenoble");
table.Rows.Add("João", 22, "Valinhos");

// Create a new writer and write the values to disk
using (CsvWriter writer = new CsvWriter(filename))
{
    writer.Write(table);
}

// Later, we could read it back from the disk using
var reader = new CsvReader(filename, hasHeaders: true);
DataTable sameTable = reader.ToTable();

It is also possible to use CsvWriter to write matrices (or jagged arrays) containing objects with mixed types:

// Tell where to store the matrix (adjust to your own liking)
string filename = Path.Combine(Path.GetTempPath(), "objects.csv");

// Let's say we want to store a jagged array of mixed types
object[][] values =
{
    new object[] { "a",    2,    3,   4 },
    new object[] {   5,  "b",    7,   8 },
    new object[] {   9,   10,  "c",  12 },
};

// Create a new writer and write the values to disk
using (CsvWriter writer = new CsvWriter(filename))
{
    writer.WriteHeaders("a", "b", "c", "d"); // this is optional
    writer.Write(values);
}

// Later, we could read it back from the disk using
var reader = new CsvReader(filename, hasHeaders: true);
object[][] sameMatrix = reader.ToJagged<object>();
See Also