5ea497b2 b873 a39d 59b3 c0347d97c20e - akesseler/Plexdata.CsvParser GitHub Wiki

CsvWriter Class

This static class allows writing a matrix of Object instances either into a file or simply into a stream (which actually can also be a file).

Inheritance Hierarchy

System.Object
  Plexdata.CsvParser.Processors.CsvWriter
Namespace: Plexdata.CsvParser.Processors
Assembly: Plexdata.CsvParser.NET (in Plexdata.CsvParser.NET.dll) Version: 1.1.3+d5bef99aa35461ce4bafadf0bf2c2aeca18044ea

Syntax

C#

public static class CsvWriter

Methods

 

Name Description
Private methodStatic member GetContentOrThrow Gets the transformed content from given container.
Public methodStatic member Write(IEnumerable(IEnumerable(Object)), Stream) This method tries to write given values into given stream.
Public methodStatic member Write(IEnumerable(IEnumerable(Object)), String) This method tries to write given values into given file.
Public methodStatic member Write(CsvContainer, Stream) This method tries to write given container into given stream.
Public methodStatic member Write(CsvContainer, String) This method tries to write given container into given file.
Public methodStatic member Write(IEnumerable(IEnumerable(Object)), Stream, CsvSettings) This method tries to write given values into given stream.
Public methodStatic member Write(IEnumerable(IEnumerable(Object)), String, CsvSettings) This method tries to write given values into given file.
Public methodStatic member Write(CsvContainer, Stream, CsvSettings) This method tries to write given container into given stream.
Public methodStatic member Write(CsvContainer, String, CsvSettings) This method tries to write given container into given file.
Public methodStatic member Write(IEnumerable(IEnumerable(Object)), String, CsvSettings, Boolean) This method tries to write given values into given file.
Public methodStatic member Write(CsvContainer, String, CsvSettings, Boolean) This method tries to write given container into given file.
Private methodStatic member WriteLine This method tries to write a particular line into given stream according to given parameter set.
  Back to Top

Remarks

CSV actually means Comma Separated Values. Sometimes it is also called as Character Separated Values. But no matter which name is used, CSV always represents a text file mainly used for data exchange between different system.

It would be possible (using a proper configuration) to write a CSV output according to the rules of RFC 4180. For more information about RFC 4180 please visit the web-site under https://www.ietf.org/rfc/rfc4180.txt

Actually, this class allows to write all CSV data as some kind of plain text. Such functionally seems to be necessary because sometimes CSV files may contain more or less data items as a fixed number of columns would allow. Therefore, this class should be helpful to process CSV files with dynamic content.

Examples

This section wants to show a simple but hopefully useful example of how to use the CSV Parser to write data.

using Plexdata.CsvParser.Constants;
using Plexdata.CsvParser.Processors;
using System;
using System.Collections.Generic;
namespace MyCsvWriter
{
    class Program
    {
        static void Main(String[] args)
        {
            try
            {
                String filename = @"C:\folder\file.csv";

                List<List<Object>> content = new List<List<Object>>
                {
                    new List<Object> { "Name", "Notes" },
                    new List<Object> { "Marley, Bob", "Jamaican singer-songwriter" },
                    new List<Object> { "Monroe, Marilyn", "American actress", "model and singer" },
                    new List<Object> { "Snipes, Wesley", "American actor", "director, film producer", "martial artist" },
                    new List<Object> { "Hurley, Elizabeth" }
                };

                // Output file would contain this content:
                // Name;Notes
                // "Marley, Bob";"Jamaican singer-songwriter"
                // "Monroe, Marilyn";"American actress";"model and singer"
                // "Snipes, Wesley";"American actor";"director, film producer";"martial artist"
                // "Hurley, Elizabeth" 

                CsvSettings settings = new CsvSettings() { Heading = true, Textual = true, Separator = ColumnSeparators.SemicolonSeparator };
                CsvWriter.Write(content, filename, settings);

                Console.ReadKey();
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }
    }
}

See Also

Reference

Plexdata.CsvParser.Processors Namespace

⚠️ **GitHub.com Fallback** ⚠️