Unosquare
    Show / Hide Table of Contents

    Class CsvWriter

    A CSV writer useful for exporting a set of objects.

    Inheritance
    Object
    CsvWriter
    Namespace: Swan.Formatters
    Syntax
    public class CsvWriter : IDisposable
    Examples

    The following code describes how to save a list of objects into a CSV file.

    using System.Collections.Generic;
    using Swan.Formatters;
    
    class Example
    {
        class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }
    
        static void Main()
        {
            // create a list of people 
            var people = new List<Person>
            {
                new Person { Name = "Artyom", Age = 20 },
                new Person { Name = "Aloy", Age = 18 }
            }
    
            // write items inside file.csv
            CsvWriter.SaveRecords(people, "C:\\Users\\user\\Documents\\file.csv");
    
            // output
            // | Name   | Age |
            // | Artyom | 20  |
            // | Aloy   | 18  |
        }
    }

    Constructors

    CsvWriter(Stream)

    Initializes a new instance of the CsvWriter class. It uses the Windows 1252 encoding and automatically closes the stream upon disposing this writer.

    Declaration
    public CsvWriter(Stream outputStream)
    Parameters
    Type Name Description
    Stream outputStream

    The output stream.

    CsvWriter(Stream, Encoding)

    Initializes a new instance of the CsvWriter class. It automatically closes the stream when disposing this writer.

    Declaration
    public CsvWriter(Stream outputStream, Encoding encoding)
    Parameters
    Type Name Description
    Stream outputStream

    The output stream.

    Encoding encoding

    The encoding.

    CsvWriter(Stream, Boolean, Encoding)

    Initializes a new instance of the CsvWriter class.

    Declaration
    public CsvWriter(Stream outputStream, bool leaveOpen, Encoding encoding)
    Parameters
    Type Name Description
    Stream outputStream

    The output stream.

    Boolean leaveOpen

    if set to true [leave open].

    Encoding encoding

    The encoding.

    CsvWriter(String)

    Initializes a new instance of the CsvWriter class. It opens the file given file, automatically closes the stream upon disposing of this writer, and uses the Windows 1252 encoding.

    Declaration
    public CsvWriter(string filename)
    Parameters
    Type Name Description
    String filename

    The filename.

    CsvWriter(String, Encoding)

    Initializes a new instance of the CsvWriter class. It opens the file given file, automatically closes the stream upon disposing of this writer, and uses the given text encoding for output.

    Declaration
    public CsvWriter(string filename, Encoding encoding)
    Parameters
    Type Name Description
    String filename

    The filename.

    Encoding encoding

    The encoding.

    Properties

    Count

    Gets number of lines that have been written, including the headings line.

    Declaration
    public ulong Count { get; }
    Property Value
    Type Description
    UInt64

    The count.

    EscapeCharacter

    Gets or sets the escape character to use to escape field values.

    Declaration
    public char EscapeCharacter { get; set; }
    Property Value
    Type Description
    Char

    The escape character.

    IgnorePropertyNames

    Defines a list of properties to ignore when outputting CSV lines.

    Declaration
    public List<string> IgnorePropertyNames { get; }
    Property Value
    Type Description
    List<String>

    The ignore property names.

    NewLineSequence

    Gets or sets the new line character sequence to use when writing a line.

    Declaration
    public string NewLineSequence { get; set; }
    Property Value
    Type Description
    String

    The new line sequence.

    SeparatorCharacter

    Gets or sets the field separator character.

    Declaration
    public char SeparatorCharacter { get; set; }
    Property Value
    Type Description
    Char

    The separator character.

    Methods

    Dispose()

    Declaration
    public void Dispose()

    Dispose(Boolean)

    Releases unmanaged and - optionally - managed resources.

    Declaration
    protected virtual void Dispose(bool disposeAlsoManaged)
    Parameters
    Type Name Description
    Boolean disposeAlsoManaged

    true to release both managed and unmanaged resources; false to release only unmanaged resources.

    SaveRecords<T>(IEnumerable<T>, Stream, Boolean)

    Saves the items to a stream. It uses the Windows 1252 text encoding for output.

    Declaration
    public static int SaveRecords<T>(IEnumerable<T> items, Stream stream, bool truncateData = false)
    Parameters
    Type Name Description
    IEnumerable<T> items

    The items.

    Stream stream

    The stream.

    Boolean truncateData

    true if stream is truncated, default false.

    Returns
    Type Description
    Int32

    Number of item saved.

    Type Parameters
    Name Description
    T

    The type of enumeration.

    SaveRecords<T>(IEnumerable<T>, String)

    Saves the items to a CSV file. If the file exits, it overwrites it. If it does not, it creates it. It uses the Windows 1252 text encoding for output.

    Declaration
    public static int SaveRecords<T>(IEnumerable<T> items, string filePath)
    Parameters
    Type Name Description
    IEnumerable<T> items

    The items.

    String filePath

    The file path.

    Returns
    Type Description
    Int32

    Number of item saved.

    Type Parameters
    Name Description
    T

    The type of enumeration.

    WriteHeadings(IDictionary)

    Writes the headings.

    Declaration
    public void WriteHeadings(IDictionary dictionary)
    Parameters
    Type Name Description
    IDictionary dictionary

    The dictionary to extract headings.

    Exceptions
    Type Condition
    ArgumentNullException

    dictionary.

    WriteHeadings(Object)

    Writes the headings.

    Declaration
    public void WriteHeadings(object obj)
    Parameters
    Type Name Description
    Object obj

    The object to extract headings.

    WriteHeadings(Type)

    Writes the headings.

    Declaration
    public void WriteHeadings(Type type)
    Parameters
    Type Name Description
    Type type

    The type of object to extract headings.

    Exceptions
    Type Condition
    ArgumentNullException

    type.

    WriteHeadings<T>()

    Writes the headings.

    Declaration
    public void WriteHeadings<T>()
    Type Parameters
    Name Description
    T

    The type of object to extract headings.

    WriteLine(IEnumerable<Object>)

    Writes a line of CSV text. Items are converted to strings. If items are found to be null, empty strings are written out. If items are not string, the ToStringInvariant() method is called on them.

    Declaration
    public void WriteLine(IEnumerable<object> items)
    Parameters
    Type Name Description
    IEnumerable<Object> items

    The items.

    WriteLine(IEnumerable<String>)

    Writes a line of CSV text. If items are found to be null, empty strings are written out.

    Declaration
    public void WriteLine(IEnumerable<string> items)
    Parameters
    Type Name Description
    IEnumerable<String> items

    The items.

    WriteLine(Object[])

    Writes a line of CSV text. Items are converted to strings. If items are found to be null, empty strings are written out. If items are not string, the ToStringInvariant() method is called on them.

    Declaration
    public void WriteLine(params object[] items)
    Parameters
    Type Name Description
    Object[] items

    The items.

    WriteLine(String[])

    Writes a line of CSV text. If items are found to be null, empty strings are written out.

    Declaration
    public void WriteLine(params string[] items)
    Parameters
    Type Name Description
    String[] items

    The items.

    WriteObject(Object)

    Writes a row of CSV text. It handles the special cases where the object is a dynamic object or and array. It also handles non-collection objects fine. If you do not like the way the output is handled, you can simply write an extension method of this class and use the WriteLine method instead.

    Declaration
    public void WriteObject(object item)
    Parameters
    Type Name Description
    Object item

    The item.

    Exceptions
    Type Condition
    ArgumentNullException

    item.

    WriteObject<T>(T)

    Writes a row of CSV text. It handles the special cases where the object is a dynamic object or and array. It also handles non-collection objects fine. If you do not like the way the output is handled, you can simply write an extension method of this class and use the WriteLine method instead.

    Declaration
    public void WriteObject<T>(T item)
    Parameters
    Type Name Description
    T item

    The item.

    Type Parameters
    Name Description
    T

    The type of object to write.

    WriteObjects<T>(IEnumerable<T>)

    Writes a set of items, one per line and atomically by repeatedly calling the WriteObject method. For more info check out the description of the WriteObject method.

    Declaration
    public void WriteObjects<T>(IEnumerable<T> items)
    Parameters
    Type Name Description
    IEnumerable<T> items

    The items.

    Type Parameters
    Name Description
    T

    The type of object to write.

    Comments

    Back to top Copyright © 2017-2019 Unosquare