CsvWriter Class |
Namespace: Accord.IO
The CsvWriter type exposes the following members.
Name | Description | |
---|---|---|
CsvWriter(TextWriter, Char) |
Initializes a new instance of the CsvWriter class.
| |
CsvWriter(String, Char) |
Initializes a new instance of the CsvWriter class.
|
Name | Description | |
---|---|---|
Comment |
Gets or sets the comment character indicating that a line is commented out.
| |
Delimiter |
Gets or sets the delimiter character separating each field.
| |
Escape |
Gets or sets the escape character letting insert quotation characters inside a quoted field.
| |
FormatProvider |
Gets or sets the format provider to use when converting
data-types to text representations. Default is to use
CultureInfo.InvariantCulture.
| |
Quote |
Gets or sets the quotation character wrapping every field.
| |
Writer |
Gets the writer.
|
Name | Description | |
---|---|---|
Dispose |
Performs application-defined tasks associated with
freeing, releasing, or resetting unmanaged resources.
| |
Dispose(Boolean) |
Releases unmanaged and - optionally - managed resources.
| |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
Finalize |
Finalizes an instance of the CsvWriter class.
(Overrides ObjectFinalize.) | |
GetHashCode | Serves as the default hash function. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
ToString | Returns a string that represents the current object. (Inherited from Object.) | |
ToText |
Initializes a new instance of the CsvWriter
class to write the CSV fields to a in-memory string.
| |
Write(DataTable) |
Writes the specified table in a CSV format.
| |
WriteT(T) |
Writes the specified matrix in CSV format.
| |
WriteT(T) |
Writes the specified matrix in CSV format.
| |
WriteHeaders(DataTable) |
Writes the column names of a data table as the headers of the CSV file.
| |
WriteHeaders(String) |
Writes the column names of a data table as the headers of the CSV file.
| |
WriteLineT(T) |
Writes the specified fields in a CSV format.
| |
WriteLineT(T, String) |
Writes the specified fields in a CSV format.
|
Name | Description | |
---|---|---|
HasMethod |
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.) | |
IsEqual |
Compares two objects for equality, performing an elementwise
comparison if the elements are vectors or matrices.
(Defined by Matrix.) | |
To(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.) | |
ToT | 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.) |
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>();